Thanks for the suggestion novacain. I tried that though, and seem to be having the same problem. Again, the problem appears to be my new thread is NOT starting immediately and gaining CPU time. It only starts after the code that gets executed (processing of the file) when the user clicks the "batch" button. Here is a scaled down version of some of my code that might help explain the problem.
First off, I'm coding in C in Visual Studio 2005.
The user clicks a "batch" button on a window and this code starts
Code:
/*
*
* some code here
*
*/
CASE IDM_BATCH
ghCancelEvent = CreateEvent(NULL, TRUE, FALSE, TEXT("CancelEvent"));
CreateThread(NULL, 0, startBatchStatus, NULL, 0, &dwThreadID);
/*
*
* code to process file of records
*
*
*/
Here is the thread procedure startBatchStatus:
Code:
DWORD WINAPI startBatchStatus( void )
{
/*
*
* some code here
*
*/
hwndStatThread = CreateDialog(ghInstance, "Batch_Status", hComWnd, BatchStatusDlgProc);
while(GetMessage(&Msg, hwndStatThread, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
Here is code that handles messages to the dialog:
Code:
BOOL CALLBACK BatchStatusDlgProc( HWND hDlg, UINT wMessage, WPARAM wParam, LPARAM lParam )
{
/*
*
* some code here
*
*
*/
case IDC_CANCEL :
MessageBox( NULL, "Canceled process", "User Error", MB_SYSTEMMODAL | MB_ICONHAND | MB_OK );
gbCancel_Button = TRUE;
EndDialog ( hDlg, 0 );
return( 0 );
break;
/*
*
* some code here
*
*
*/
}
The dialog eventually appears, but only after the code to process the records is complete. And when I click the cancel button on the dialog, the dialog ends properly, so the message loop is working properly. I even put a Sleep(10000); and break; immediately after the CreateThread() command to eliminate the file processing code altogether, and sure enough, there's about a 10 second pause after you click the "batch" button, then the dialog appears. In other words, the dialog didn't appear until the Sleep() and break commands were finished.
So I'm pretty much back to my original question. When you create a new thread in a block of code, whether using CreateThread() or _beginthreadex(), does the thread start IMMEDIATELY after you issue the command and start sharing CPU time with the main thread? It certainly doesn't appear so in my case. I would think that what I'm trying to do (start a long process and provide a dialog to cancel it) would be a pretty common thing. But it's driving me nuts.
Originally Posted by
novacain
Use events and wait objects.
Use CreateEvent() to create a 'cancel' event, create it set to 'not_cancelled' (ie false). Do this before you start the thread working.
Make the thread periodically check for the 'cancel' event (ie each record) with WaitForSingleObject() in the thread processing function.
If the user cancels the processing use SetEvent() to change the cancel event to 'canceled' (ie true).
If the thread finds it has been canecled call the threads clean up code etc.
Here is some code;
Windows SDK Thread: How to end a thread?
Another way is a mutex.
Create a global (in C) variable that the parent dialog and thread can access.
The parent dlg sets it to cancelled, and the thread periodically checks the value.
This requires critical sections to ensure only one thread can access this variable at one time.