Re: how to end a thread from the main thread



The trick goes like this...

Suppose for this example I have a boolean, ThreadRunning, on behalf of a view. Also a
boolean called closing. Both are initialized to false, and ThreadRunning is set to TRUE
when the thread starts

void CMyView::OnClose()
{
if(ThreadRunning)
{
... initiate thread shutdown
closing = TRUE;
}
CView::OnClose();
}


LRESULT CMyView::OnThreadFinished(WPARAM, LPARAM)
{
ThreadRunning = FALSE;
if(closing)
PostMessage(WM_CLOSE);
return 0;
}

Often I'll add ON_UPDATE_COMMAND_UI handlers that do things like

pCmdUI->Enable(!closing && ...);

I've been burned badly with the WaitFor approach, often because it blocks the thread,
usually doing things lilke hanging the Internet connections, or something like that.
joe

On 21 Dec 2005 08:49:00 -0800, "Josh McFarlane" <darsant@xxxxxxxxx> wrote:

>Joseph M. Newcomer wrote:
>> See my essay on worker threads on my MVP Tips site.
>>
>> Be cautious about waiting on the thread handle; I've largely abandoned this as a way of
>> telling that the thread has terminated. Instead, I let the thread PostMessage a
>> notification to my main UI thread that it has terminated.
>> joe
>
>Just out of curiosity, how would you synchronize a shutdown process?
>
>The only place I ever truely wait on the handle is on shutdown events
>of my data collection programs, where the workflow needs to be as
>follows
>
>User initiates shutdown
>Signal hardware collection thread shutdown
>Wait on thread(s)
>Signal processing threads to finish with queue and shutdown
>Wait on thread(s)
>Signal network transmission buffer to disconnect once all data has been
>pushed
>Post another WM_CLOSE
>Return to queue to process any messages the above threads have posted
>to the main app.
>On second reentry, shutdown app.
>
>The rest of the thread-shutdown cases are handled as you said, with
>main UI thread notification from the handler.
>
>Josh McFarlane
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • PC based robot design -- musings
    ... As I am working on the robot software. ... The task objects have only a few virtul API entry points, ... Boolean Init() ... Shutdown function is called. ...
    (comp.robotics.misc)
  • Re: WXP does not automatically turn off the computer
    ... >have a UPS setup but it has its own software package and seems to be working ... See MVP Jim Eshelman's Shutdown Troubleshooter for Windows XP at ... Microsoft MVP ...
    (microsoft.public.windowsxp.basics)
  • Re: XP Shutdown freeze NO LUCK
    ... Microsoft Windows MVP ... > freezes on the BlueScreenOfDeath. ... >>> Must hit power button to shutdown manually. ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Doing Program Cleanup at System Shutdown
    ... Klaus H. Probst, MVP ... "HarveyB" wrote in message ... > I have a C# program that runs a logging function and sits on the System tray. ... It also works intermittantly when the system is shutdown. ...
    (microsoft.public.dotnet.general)
  • Re: SNAPI Floundering App – Joe Please Help
    ... PeekMessage loop gives the illusion the program is still operational, ... power manager scheduling a shutdown. ... In many of those articles ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)