Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

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: http://computer-programming-forum.com/82-mfc/1d8b6d604dd7c393.htm

Code:

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

while(true)
{
// Plotting graph
// Requires Update
appPtr->UpdatePoints(i);
pStatic->PostMessage(WM_PAINT);
}
}
Posted
Updated 30-Apr-13 23:13pm
v5
Comments
Leo Chapiro 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

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.

Why?

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.
 
Share this answer
 
v2
Comments
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...
Leo Chapiro 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900