Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: graphics MFC/C++
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:
 
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 29-Apr-13 23:11pm
Edited 30-Apr-13 23:13pm
v5
Comments
du[DE] at 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 at 30-Apr-13 5:34am
   
Its a WorkerThread... Updated my code section...

1 solution

Rate this: bad
good
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.
 
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.
  Permalink  
v2
Comments
Mobile.Instinct at 30-Apr-13 5:56am
   
Is there a way to get only the Picture control to update? Outside of the thread I mean...
du[DE] at 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 at 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
0 OriginalGriff 405
1 Sergey Alexandrovich Kryukov 220
2 Peter Leow 215
3 Maciej Los 195
4 DamithSL 174


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