Click here to Skip to main content
13,591,889 members
Rate this:
Please Sign up or sign in to vote.
See more:

I have been working on a multi-threaded MFC application that needs to plot a graph on a picture control. Everything seems

to work fine apart from the fact the picture control does not redraw on calling the Invalidate function for the picture

control. The whole window gets redrawn and flicker occurs if I call Invalidate on the dialog. Any suggestions on where I

am going wrong? I have tried this but it did not help:


DWORD WINAPI CDlg::WorkerThread(LPVOID lpVoid)
CDlg* appPtr = (CDlg*)lpVoid;
CStatic* pStatic = (CStatic*)appPtr->GetDlgItem(IDC_GRAPH_WINDOW);

// Plotting graph
// Requires Update
Posted 29-Apr-13 23:11pm
Updated 30-Apr-13 23:13pm
_duDE 30-Apr-13 5:24am
Oo, if you have a worker thread you can't try to access a GUI! You can send (or better post) a message to the main thread but never ever doing that from another thread! Please explain what kind of thread is the Thread(...)
Mobile.Instinct 30-Apr-13 5:34am
Its a WorkerThread... Updated my code section...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think the problem is the acces of GUI from the worker thread. Take a look at this article to see how to do right way: Using Worker Threads[^]

Worker threads and the GUI II: Don't touch the GUI

That's right. A worker thread must not touch a GUI object. This means that you should not query the state of a control, add something to a list box, set the state of a control, etc.


Because you can get into a serious deadlock situation. A classic example was posted on one of the discussion boards, and it described something that had happened to me last year. The situation is this: you start a thread, and then decide to wait for the thread to complete. Meanwhile, the thread does something apparently innocuous, such as add something to a list box, or, in the example that was posted, calls FindWindow. In both cases, the process came to a screeching halt, as all threads deadlocked.
Mobile.Instinct 30-Apr-13 5:56am
Is there a way to get only the Picture control to update? Outside of the thread I mean...
_duDE 30-Apr-13 6:04am
As I mentioned, I would make PostMessage from the worker thread to the main thread. The main (GUI) thread should make the update then.
Mobile.Instinct 1-May-13 5:12am
something like what I posted? (Updated the code part in the question)...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.180618.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100