Re: how to end a thread from the main thread
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 22 Dec 2005 15:23:04 -0500
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
.
- Follow-Ups:
- Re: how to end a thread from the main thread
- From: Michael Nielsen
- Re: how to end a thread from the main thread
- References:
- how to end a thread from the main thread
- From: Mystique
- Re: how to end a thread from the main thread
- From: Josh McFarlane
- Re: how to end a thread from the main thread
- From: Joseph M . Newcomer
- Re: how to end a thread from the main thread
- From: Josh McFarlane
- how to end a thread from the main thread
- Prev by Date: Re: Making sure data is saved on closing application
- Next by Date: Re: Making sure data is saved on closing application
- Previous by thread: Re: how to end a thread from the main thread
- Next by thread: Re: how to end a thread from the main thread
- Index(es):
Relevant Pages
|