Re: Terminating a worker thread

Tech-Archive recommends: Fix windows errors by optimizing your registry



That worked just fine! Thanks.

"Chris Becke" <chris.becke@xxxxxxxxx> wrote in message
news:e1NVZ6L0GHA.1252@xxxxxxxxxxxxxxxxxxxxxxx
"Allan Phillips" <allanephillips@xxxxxxxxx> wrote in message
news:eP$lA1E0GHA.5048@xxxxxxxxxxxxxxxxxxxxxxx
I have created a second thread from my CDoc class that does some
background
processing.

Everything seems to work fine unless I destroy the document before the
second thread has finished and exited.

I would like to kill my worker (second) thread as part of my document
destructor. What do I need to do to have that happen?

This depends on how you tell your worker thread what to do. Windows cannot
cleanly kill worker threads for you, they have to terminate properly so
that
objects get cleaned up, etc.

So, whatever the worker thread is stuck doing, you need to signal it to
abort its operation, and exit.

For example in my application I use a worker thread that i post thread
messages to. The worker thread therefore has a message pump. To destroy
the
worker thread I use the following code:

PostThreadMessage(dwThreadId,WM_QUIT,0,0);
WaitForSingleObject(hThread,INFINITE);

The first line tells the worker thread to terminate when it gets back to
the
message pump. The next line tells my main thread to wait, until the worker
thread has actually been destroyed.




.



Relevant Pages

  • Re: MFC and Worker Thread issue
    ... You could hide the window when Cancel is pressed, wait for your heavy job to finish, then destroy the dialog. ... I have to go into Task Manager and terminate the first instance. ... I would just do something simple like immediately disable the Cancel button after it is clicked, but leave the dialog still showing until the worker thread has finished. ...
    (microsoft.public.vc.mfc)
  • Re: PostThreadMessage just gets last message
    ... virtual inline bool ThreadOnReceived; ... You would not terminate a worker thread this way. ... own Windows 1.0 message pump in a worker thread! ... SQTrdMsg* pM = new SQTrdMsg; ...
    (microsoft.public.vc.mfc)
  • Re: UI Thread V Worker thread
    ... and have the worker thread block until the dialog is acknowledged so ... wrappers to play several audio files in response to still other asynchronous ... creating a new instance of a form and calling ShowDialog() is that since the ... I even worried about keeping the message pump running on the main UI if I ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Threads
    ... only GUI programming and there would be a message pump in the application ... > original thread, how do you inquire the status of the new "watcher" ... > have no way to pass why it died back to your original caller, ... > State management of the worker thread requires at least one way ...
    (microsoft.public.vc.mfc)
  • Re: UI Thread V Worker thread
    ... a worker thread STA. ... > Thanks for the response Dave and Herfried. ... >> main UI thread and also my audio thread both rely on window messages ... >> This fixed the message pump issue, because the new form has it's own ...
    (microsoft.public.dotnet.framework.windowsforms.controls)