Re: Terminate worker thread when Dialog closes

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Si wrote:
I have a CDialog that spawns a worker thread. How do I terminate the thread when the dialog box closes (and the CDialog class is destroyed)?

Signal the worker thread to close itself, and then wait until it has closed.


If the thread is executing a loop you can signal it by setting a bool that it tests. If the thread is suspended by a WaitFor... call then use an event (CreateEvent, SetEvent) in the list of things it is waiting for. When the thread senses the signal it must return from the thread function.

The dialog code should send the signal, then call WaitForSingleObject with the thread handle. The wait makes sure the dialog is not destroyed while the thread is accessing dialog data. WaitForSingleObject will return after the thread has exited, then you can continue to destroy the dialog.

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • RE: While Loop with a StreamReader process included for fun
    ... 'No need to destroy the file reader it is automatically destroyed by ... "William Foster" wrote: ... I am trying to write a process that uses a while loop to cycle multiple ... before running through the While loop for a second time it will not work, ...
    (microsoft.public.dotnet.general)
  • Re: When to call DestroyIcon ?
    ... If the icon does not need to persist then destroy ... unless the icon is a system icon which the system will take ... it in a loop, or is it OK to just call it after my loop is finished? ...
    (microsoft.public.vb.winapi.graphics)
  • RE: While Loop with a StreamReader process included for fun
    ... You are currently dimming a single streamreader, not initializing it, and ... then initializing it over and over in your loop. ... 'No need to destroy the file reader it is automatically destroyed by ... I am trying to write a process that uses a while loop to cycle multiple ...
    (microsoft.public.dotnet.general)
  • link list -is this enough?
    ... loop i want to destroy this link list. ... Posted via Mailgate.ORG Server - http://www.Mailgate.ORG ...
    (comp.lang.c)
  • Clearing all elements from within a named table.
    ... I would like to wipe ... I have tried to destroy the objects themselves, ... the variable pointing to the object (which I thought would delete the ... Please could someone suggest a method I can use to loop through all of ...
    (comp.lang.javascript)