Click to See Complete Forum and Search --> : invalidate() from workerThread problem


Sickboy
November 5th, 2004, 10:17 AM
I've got a worker thread which invalidates my Window-Screen and get a message from On_Paint if painting is done.

My thread works in this way:

UINT myThread::ThreadMain()
{
for (int i = 0; i < TrialAnzahl; i++)
{
p_dialog->Trial = i;
bDCPainted = false;
if (iMissedTrials==p_daten->iMaxMissingTrials)
{
AfxMessageBox(text, NULL, MB_OK);
iMissedTrials = 0;
}
Timer.starttimer();
p_dialog->Invalidate();
while(!bDCPainted){};
Timer.stoptimer();
dur = Timer.timeintv();
Timer.startMyWaitableTimer(dDisplayInterval-dur);
}
}

my OnPaint like this:

void CPoffenbergMFCDlg::OnPaint()
{

if (IsIconic())
{...}
else
{
Display(Trial);
meinThread.bDCPainted = true;
}
}


If i release my application i get following problem:
- Sometimes it happens that p_dialog->Invalidate(); has been called but bDCPainted isn't true, so that my Thread hang at while(!bDCPainted){};

In debug mode it works :confused:

Anyone knows a better (working) way ?


thx
daSickboy

kuphryn
November 5th, 2004, 10:38 AM
In general, process GUI objects within context of the UI thread. One solution is messages.

Kuphryn

cilu
November 5th, 2004, 10:43 AM
As Kuphryn said, you should use messages to communicate between thread. Or at least you can replace this

while(!bDCPainted){};

with a call to WaitForSingleObject(). Take a look in MSDN about synchronization.

Sickboy
November 5th, 2004, 10:45 AM
Can you concretize your suggestion regarding my problem ?

Thanks
daSickboy

Sickboy
November 5th, 2004, 10:51 AM
Ok i will look for WaitForSingleObject() and SendMessage() if it should work in this way.
But it would also interest me why my solution doesn´t work.
Can you explain it ?

Thanks
daSickboy

Andreas Masur
November 5th, 2004, 11:03 AM
[ Moved thread ]

Andreas Masur
November 5th, 2004, 11:04 AM
But it would also interest me why my solution doesn´t work.
Can you explain it ?
The answer can be found in the following FAQ (http://www.codeguru.com/forum/showthread.php?t=312454)...

Sickboy
November 5th, 2004, 01:53 PM
Now i use setEvent(),ResetEvent() to tell the thread that drawing in On_Paint() is done.
And i try to use PostMessage to tell the dialog to draw something (from thread).
The problem is the second time i call

if(!PostMessage(*hDialogWnd,WM_PAINT,0,0))AfxMessageBox("Failed",NULL,MB_OK);

it fails.

How can i check what is wrong?

thx

MikeAThon
November 5th, 2004, 08:53 PM
Don't PostMessage a WM_PAINT message directly.

Instead, PostMessage a user-defined message that the dialog handles (in its own thread) by invalidating itself.

Incidentally, if PostMessage fails, then you should call GetLastError to find out why.