Re: MFC and threads
I should've mentioned some more details. Here goes.
Ok there is a main gui window that creates a worker thread. Worker
thread executes its code. Based on some logical decisions it needs to
pause to display user a message. Worker thread sends user defined
message via PostMessage. The message handler instantiates/creates
another window. Its something like this:
// PostMessage Handler for pause message
LRESULT CMainGui::OnPause(WPARAM w,LPARAM p)
{
Dummy dlg;
dlg.Create(IDD_DUMMY_DIALOG); // Create an instance of message
window
dlg.ShowWindow(SW_SHOW); // display the window to display
the message, Hello There!, in Edit control
return 0; // now window
appears with Resume button on bottom-so far so good
}
// Worker Thread Code
LRESULT CMainGuiDlg::WorkerThread(WPARAM , LPARAM)
{
// Executes code below--everything is good
// Some decision making happens and this worker thread code
must pause!
pause =
TRUE; // pause
defined as BOOL in CMAINGui header file
// hEvent defined to be of HANDLE datatype in CMainGuiDlg header
file
hEvent = CreateEvent(0, TRUE, TRUE, "MyEvent");
// i check all functions to see if they were successful which
they are(code not listed here)
ResetEvent(hEvent); // set the event state to non-signaled
PostMessage(UWM_PAUSE, 0, 0); // OnPause is the message handler
^above^ for UWM_PAUSE message
while ( running && paused )
{
if (paused)
{
switch(::WaitForSingleObject(hEvent, 1000))
{
case WAIT_OBJECT_0:
break;
case WAIT_TIMEOUT:
continue;
}
}
}
// Upon clicking Resume in new dialog window execution must start
here!
PostMessage(UWM_STATUS, 0, "Resuming...");
....
....
return 0; // End of Worker thread code
}
Now, the new window that was opened, it has a Resume button. It
displays a message to user upon its intialization. I see the
messsage. Everything is good so far. Now after reading the message,
I click Resume and I want my worker thread to continue. That is, break
out of that while loop above. Now, hell breaks loose. This is the
code that runs when I click Resume button.
void DUMMY::OnResume()
{
// TODO: Add your control notification handler code here
CMainGuiDlg dlg; << Primary window which launched Dummy
dialog Window- i create this data type so i can acess its member
variables, namely, pause and hEvent
dlg.pause = FALSE; << paused defined in CMainGuiDlg as
BOOL ..since user read the message, user wants to resume the execution
of program so pause is set to FALSE now.
// sets the state of the specified event object to signaled.
if ( ! ::SetEvent(dlg.hEvent) ) // The event signal is being
processed inside that while loop above
{
AfxMessageBox("SetEvent not successful");
}
// User is done reading the message-Close the window
EndDialog(0);
}
This is where my program crashes. EndDialog generates Debug Assertion
Failure message. SetEvent fails. Therefore, worker thread code never
gets out of while loop. If more details are required, I'll try to be
more specific. Thanks