Re: checking if thread has ended
This is a multi-part message in MIME format.
--------------090600020801040504040807
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Joseph M. Newcomer wrote:
See below...
On Tue, 29 Jul 2008 04:46:00 +0530, Ashutosh <smbs-msdn@nospam.nospam> wrote:
Hi,
I am creating a background thread using AfxBeginThread in suspended state, then setting the auto delete member to false then resuming the thread.
***
Yes, this is the standard way to do it.
****
Now from the main thread when I call WaitForSingleObject with the thread handle and infinite timeout, it doesn't return even after the thread procedure has ended/returned
***
This would be a serious design error. You should not block the main thread "waiting" for
a secondary thread. Instead, have the thread PostMessage a notification to the main GUI
thread that says "I am done". You don't need to WFSO at all. For that matter, there is
probably no reason to set m_bAutoDelete to FALSE since there is no reason you should have
to care about whether or not the object is deleted.
*****
So, how do I check if the thread has terminated? I can use some variable to indicate that, but is there a way to find this using the CWinThread object returned?
****
Since you have not shown the WFSO code, so it is hard to tell what might have gone wrong,
but it should certainly continue after the thread terminates. HOWEVER, if you attempted
to manipulate any GUI object in the main thread, then the secondary thread will hang
forever because the main thread is blocked, and therefore the WFSO will never terminate
because the secondary thread is blocked by the WFSO.
So
(a) make sure that you DO NOT touch ANY control whatsoever from your thread
(b) Get rid of the WFSO entirely and the setting of m_bAutoDelete, and replace them with a
PostMessage to the main GUI thread.
Read my essays on threading on my MVP Tips site.
joe
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Thanks Doug/Joseph for your comment!!!
I was calling WFSO on the WM_CLOSE message before the CDIalog::OnClose
(make sense) and for other communications I was/am using
PostMessage/SendMessage only!!! It was in the WM_CLOSE message handler
the code didn't return from WFSO. I am doing this just to make sure that
the background thread is not killed when the main window/thread is
destroyed.
Isn't this the correct way??
The code is like this
this->ShowWindow(SW_HIDE);
if(pWorkerThread)
{
WaitForSingleObject(pWorkerThread->m_hThread, INFINITE);
}
CDialog::OnClose();
--------------090600020801040504040807
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<br>
Joseph M. Newcomer wrote:
<blockquote cite="mid:khvs84d8mptjcudd8mn1kjd11aanon25rf@4ax.com"
type="cite">
<pre wrap="">See below...
On Tue, 29 Jul 2008 04:46:00 +0530, Ashutosh <a class="moz-txt-link-rfc2396E" href="mailto:smbs-msdn@nospam.nospam"><smbs-msdn@nospam.nospam></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hi,
I am creating a background thread using AfxBeginThread in suspended state, then setting the auto delete member to false then resuming the thread.
</pre>
</blockquote>
<pre wrap=""><!---->***
Yes, this is the standard way to do it.
****
</pre>
<blockquote type="cite">
<pre wrap="">Now from the main thread when I call WaitForSingleObject with the thread handle and infinite timeout, it doesn't return even after the thread procedure has ended/returned
</pre>
</blockquote>
<pre wrap=""><!---->***
This would be a serious design error. You should not block the main thread "waiting" for
a secondary thread. Instead, have the thread PostMessage a notification to the main GUI
thread that says "I am done". You don't need to WFSO at all. For that matter, there is
probably no reason to set m_bAutoDelete to FALSE since there is no reason you should have
to care about whether or not the object is deleted.
*****
</pre>
<blockquote type="cite">
<pre wrap="">So, how do I check if the thread has terminated? I can use some variable to indicate that, but is there a way to find this using the CWinThread object returned?
</pre>
</blockquote>
<pre wrap=""><!---->****
Since you have not shown the WFSO code, so it is hard to tell what might have gone wrong,
but it should certainly continue after the thread terminates. HOWEVER, if you attempted
to manipulate any GUI object in the main thread, then the secondary thread will hang
forever because the main thread is blocked, and therefore the WFSO will never terminate
because the secondary thread is blocked by the WFSO.
So
(a) make sure that you DO NOT touch ANY control whatsoever from your thread
(b) Get rid of the WFSO entirely and the setting of m_bAutoDelete, and replace them with a
PostMessage to the main GUI thread.
Read my essays on threading on my MVP Tips site.
joe
Joseph M. Newcomer [MVP]
email: <a class="moz-txt-link-abbreviated" href="mailto:newcomer@flounder.com">newcomer@flounder.com</a>
Web: <a class="moz-txt-link-freetext" href="http://www.flounder.com">http://www.flounder.com</a>
MVP Tips: <a class="moz-txt-link-freetext" href="http://www.flounder.com/mvp_tips.htm">http://www.flounder.com/mvp_tips.htm</a>
</pre>
</blockquote>
Thanks Doug/Joseph for your comment!!!<br>
<br>
I was calling WFSO on the WM_CLOSE message before the CDIalog::OnClose
(make sense) and for other communications I was/am using
PostMessage/SendMessage only!!! It was in the WM_CLOSE message handler
the code didn't return from WFSO. I am doing this just to make sure
that the background thread is not killed when the main window/thread is
destroyed.<br>
<br>
Isn't this the correct way??<br>
The code is like this<br>
<br>
<blockquote>
<blockquote>
<blockquote> this->ShowWindow(SW_HIDE);<br>
if(pWorkerThread)<br>
{<br>
WaitForSingleObject(pWorkerThread->m_hThread, INFINITE);<br>
}<br>
CDialog::OnClose();<br>
</blockquote>
</blockquote>
</blockquote>
</body>
</html>
--------------090600020801040504040807--