Killing worker thread
Posted on 2004-07-31
I want to kill a worker thread that is hanging with waitForSingleObject.
I want to kill it from the parent dialog.
Basically I created a thread using CreateThread.
In that thread I call ShellExecute to run another application.
I used WaitForSingleObject INFINITY to cause my app to
wait for the results of the external application.
The external application creates a file. The WaitForSingleObject
is necessary because the next line of code in the thread uses that
file to load a list box.
The thread was neccessary becaue I wanted to display an animated
Please wait dialog. This dialog is modless.
I believe I needed it to be modless because when it was modal it
simply stalled the other processes from going.
So basically a function in my main dialog creates two threads.
The modeless dialog and the Thread.
When the Thread ends it uses SendMessage to close the Please wait dialog
Also it uses a pointer that was passed to lparam that allows it to call
a function to load the list box.
res is the return value of ShellExecute.
if(SendMessage( pParms->PleaseHWND, WM_CLOSE, 0, 0 )!=0)
MessageBox(NULL,"failed to call wm_close","Error",MB_OK);
Then the thread func returns zero. When done normally.
But it seems that I can not close the thread early if I add a button to
the Please Wait dialog to instruct the thread to close early.
I am stuck not knowing what to do. I need the thread to call the application up to create the text file for the listbox to use. I would like to have a button on the please wait dialog to stop in case this is not what the user wants.
I am wondering now if I should have used afxthread thing or should I have a GUI thread?
Any comments apreciated.