thread #2

when I use a thread in C++ I generally do this :

DWORD dwThreadId = 0;
 m_hRecordThread = ::CreateThread(0,0,RecordingThread,
(LPVOID)this,0,&dwThreadId);

DWORD WINAPI CEnrollBioDlg::RecordingThread(LPVOID lpArg)
{
 CEnrollBioDlg* pThis = reinterpret_cast<CEnrollBioDlg*>(lpArg);
 return pThis->RecordingThread();
}

DWORD CEnrollBioDlg::RecordingThread(void)
{
blabla ........

return 0; // THREAD returns
}

What I am wondering is when the thread returns does it still exist and in
this case do i need to explicitly call a function to terminate it or is ok
like this.
And if I need to kill it from where do I need to do it ?



0
tricubes (61)
1/22/2004 1:08:30 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
477 Views

Similar Articles

[PageSpeed] 36

"mosfet" <tricubes@wanadoo.fr> wrote in message
news:buohsc$511$1@news-reader2.wanadoo.fr...
> when I use a thread in C++ I generally do this :
>
> DWORD dwThreadId = 0;
>  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> (LPVOID)this,0,&dwThreadId);
>
> DWORD WINAPI CEnrollBioDlg::RecordingThread(LPVOID lpArg)
> {
>  CEnrollBioDlg* pThis = reinterpret_cast<CEnrollBioDlg*>(lpArg);
>  return pThis->RecordingThread();
> }
>
> DWORD CEnrollBioDlg::RecordingThread(void)
> {
> blabla ........
>
> return 0; // THREAD returns
> }
>
> What I am wondering is when the thread returns does it still exist and in
> this case do i need to explicitly call a function to terminate it or is ok
> like this.
> And if I need to kill it from where do I need to do it ?

I'm no thread expert, but I think you still need to call CloseHandle the
CreateThread return value. Otherwise and FWIW, see this CreateThread
discussion in Jeff Richter's July 1999 MSJ column...

http://www.microsoft.com/msj/0799/Win32/Win320799.aspx

-- 
Jeff Partch [VC++ MVP]


0
jeffp (1711)
1/22/2004 1:35:30 PM
Spot on

The thread will terminate just like any other.
And just like any outher will not compleetly release all the alocated
resorces untill the handle returnd by CreateThread is closed.

news:e82EZyO4DHA.1868@TK2MSFTNGP10.phx.gbl...
> "mosfet" <tricubes@wanadoo.fr> wrote in message
> news:buohsc$511$1@news-reader2.wanadoo.fr...
> > when I use a thread in C++ I generally do this :
> >
> > DWORD dwThreadId = 0;
> >  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> > (LPVOID)this,0,&dwThreadId);
> >
> > DWORD WINAPI CEnrollBioDlg::RecordingThread(LPVOID lpArg)
> > {
> >  CEnrollBioDlg* pThis = reinterpret_cast<CEnrollBioDlg*>(lpArg);
> >  return pThis->RecordingThread();
> > }
> >
> > DWORD CEnrollBioDlg::RecordingThread(void)
> > {
> > blabla ........
> >
> > return 0; // THREAD returns
> > }
> >
> > What I am wondering is when the thread returns does it still exist and
in
> > this case do i need to explicitly call a function to terminate it or is
ok
> > like this.
> > And if I need to kill it from where do I need to do it ?
>
> I'm no thread expert, but I think you still need to call CloseHandle the
> CreateThread return value. Otherwise and FWIW, see this CreateThread
> discussion in Jeff Richter's July 1999 MSJ column...
>
> http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
>
> -- 
> Jeff Partch [VC++ MVP]
>
>


0
Dave (18)
1/22/2004 3:59:05 PM
Ok so it seems that I need to close handle associated with my thread but
from where can I close handle because when I start my thread I am in the
OnInitDialog()

BOOL CEnrollBioDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

DWORD dwThreadId = 0;
 m_hRecordThread = ::CreateThread(0,0,RecordingThread,
(LPVOID)this,0,&dwThreadId);

}



what I would like to do is :

BOOL CEnrollBioDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

DWORD dwThreadId = 0;
 m_hRecordThread = ::CreateThread(0,0,RecordingThread,
(LPVOID)this,0,&dwThreadId); start thread

WaitForSingleObject(m_evtThreadEnd); // Wait the thread sets the event
before to return
CloseHandle(hRecordThread ) // close handle
}



My problem is if I do that WaitForSingleObject will block my window because
OninitDialog won't return so how can I do ?




"Dave" <DAVE@XYZ.COM> a �crit dans le message de
news:etberCQ4DHA.2432@TK2MSFTNGP09.phx.gbl...
> Spot on
>
> The thread will terminate just like any other.
> And just like any outher will not compleetly release all the alocated
> resorces untill the handle returnd by CreateThread is closed.
>
> news:e82EZyO4DHA.1868@TK2MSFTNGP10.phx.gbl...
> > "mosfet" <tricubes@wanadoo.fr> wrote in message
> > news:buohsc$511$1@news-reader2.wanadoo.fr...
> > > when I use a thread in C++ I generally do this :
> > >
> > > DWORD dwThreadId = 0;
> > >  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> > > (LPVOID)this,0,&dwThreadId);
> > >
> > > DWORD WINAPI CEnrollBioDlg::RecordingThread(LPVOID lpArg)
> > > {
> > >  CEnrollBioDlg* pThis = reinterpret_cast<CEnrollBioDlg*>(lpArg);
> > >  return pThis->RecordingThread();
> > > }
> > >
> > > DWORD CEnrollBioDlg::RecordingThread(void)
> > > {
> > > blabla ........
> > >
> > > return 0; // THREAD returns
> > > }
> > >
> > > What I am wondering is when the thread returns does it still exist and
> in
> > > this case do i need to explicitly call a function to terminate it or
is
> ok
> > > like this.
> > > And if I need to kill it from where do I need to do it ?
> >
> > I'm no thread expert, but I think you still need to call CloseHandle the
> > CreateThread return value. Otherwise and FWIW, see this CreateThread
> > discussion in Jeff Richter's July 1999 MSJ column...
> >
> > http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
> >
> > -- 
> > Jeff Partch [VC++ MVP]
> >
> >
>
>


0
tricubes (61)
1/23/2004 11:37:28 AM
As you have the handle stored in m_hRecordThread. If the thread will have
the same life as the Dialog putting your WaitForSingleObject in the
destructor would be the simplest option.  If not you have a few option if
you have loads of threads starting and exiting you could write a app wide
thread handler, or (This would probably be the better option for a small
number) you could have the thread post an event to the window or any other
thread just before it closes and have the recipient do a WaitForSingleObject
giving it a short time out so if some thing dose go wrong it will not hang.
The time from the setting of the event to the threads handle going high
should be very short so it should not cause a performance problem.

Hope this helps.

Dave

"mosfet" <tricubes@wanadoo.fr> wrote in message
news:bur0tn$43c$1@news-reader4.wanadoo.fr...
> Ok so it seems that I need to close handle associated with my thread but
> from where can I close handle because when I start my thread I am in the
> OnInitDialog()
>
> BOOL CEnrollBioDlg::OnInitDialog()
> {
>  CDialog::OnInitDialog();
>
> DWORD dwThreadId = 0;
>  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> (LPVOID)this,0,&dwThreadId);
>
> }
>
>
>
> what I would like to do is :
>
> BOOL CEnrollBioDlg::OnInitDialog()
> {
>  CDialog::OnInitDialog();
>
> DWORD dwThreadId = 0;
>  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> (LPVOID)this,0,&dwThreadId); start thread
>
> WaitForSingleObject(m_evtThreadEnd); // Wait the thread sets the event
> before to return
> CloseHandle(hRecordThread ) // close handle
> }
>
>
>
> My problem is if I do that WaitForSingleObject will block my window
because
> OninitDialog won't return so how can I do ?
>
>
>
>
> "Dave" <DAVE@XYZ.COM> a �crit dans le message de
> news:etberCQ4DHA.2432@TK2MSFTNGP09.phx.gbl...
> > Spot on
> >
> > The thread will terminate just like any other.
> > And just like any outher will not compleetly release all the alocated
> > resorces untill the handle returnd by CreateThread is closed.
> >
> > news:e82EZyO4DHA.1868@TK2MSFTNGP10.phx.gbl...
> > > "mosfet" <tricubes@wanadoo.fr> wrote in message
> > > news:buohsc$511$1@news-reader2.wanadoo.fr...
> > > > when I use a thread in C++ I generally do this :
> > > >
> > > > DWORD dwThreadId = 0;
> > > >  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> > > > (LPVOID)this,0,&dwThreadId);
> > > >
> > > > DWORD WINAPI CEnrollBioDlg::RecordingThread(LPVOID lpArg)
> > > > {
> > > >  CEnrollBioDlg* pThis = reinterpret_cast<CEnrollBioDlg*>(lpArg);
> > > >  return pThis->RecordingThread();
> > > > }
> > > >
> > > > DWORD CEnrollBioDlg::RecordingThread(void)
> > > > {
> > > > blabla ........
> > > >
> > > > return 0; // THREAD returns
> > > > }
> > > >
> > > > What I am wondering is when the thread returns does it still exist
and
> > in
> > > > this case do i need to explicitly call a function to terminate it or
> is
> > ok
> > > > like this.
> > > > And if I need to kill it from where do I need to do it ?
> > >
> > > I'm no thread expert, but I think you still need to call CloseHandle
the
> > > CreateThread return value. Otherwise and FWIW, see this CreateThread
> > > discussion in Jeff Richter's July 1999 MSJ column...
> > >
> > > http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
> > >
> > > -- 
> > > Jeff Partch [VC++ MVP]
> > >
> > >
> >
> >
>
>


0
Dave (18)
1/23/2004 12:12:06 PM
it helps !

"Dave" <DAVE@XYZ.COM> a �crit dans le message de
news:%23Wr%23goa4DHA.1644@TK2MSFTNGP10.phx.gbl...
> As you have the handle stored in m_hRecordThread. If the thread will have
> the same life as the Dialog putting your WaitForSingleObject in the
> destructor would be the simplest option.  If not you have a few option if
> you have loads of threads starting and exiting you could write a app wide
> thread handler, or (This would probably be the better option for a small
> number) you could have the thread post an event to the window or any other
> thread just before it closes and have the recipient do a
WaitForSingleObject
> giving it a short time out so if some thing dose go wrong it will not
hang.
> The time from the setting of the event to the threads handle going high
> should be very short so it should not cause a performance problem.
>
> Hope this helps.
>
> Dave
>
> "mosfet" <tricubes@wanadoo.fr> wrote in message
> news:bur0tn$43c$1@news-reader4.wanadoo.fr...
> > Ok so it seems that I need to close handle associated with my thread but
> > from where can I close handle because when I start my thread I am in the
> > OnInitDialog()
> >
> > BOOL CEnrollBioDlg::OnInitDialog()
> > {
> >  CDialog::OnInitDialog();
> >
> > DWORD dwThreadId = 0;
> >  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> > (LPVOID)this,0,&dwThreadId);
> >
> > }
> >
> >
> >
> > what I would like to do is :
> >
> > BOOL CEnrollBioDlg::OnInitDialog()
> > {
> >  CDialog::OnInitDialog();
> >
> > DWORD dwThreadId = 0;
> >  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> > (LPVOID)this,0,&dwThreadId); start thread
> >
> > WaitForSingleObject(m_evtThreadEnd); // Wait the thread sets the event
> > before to return
> > CloseHandle(hRecordThread ) // close handle
> > }
> >
> >
> >
> > My problem is if I do that WaitForSingleObject will block my window
> because
> > OninitDialog won't return so how can I do ?
> >
> >
> >
> >
> > "Dave" <DAVE@XYZ.COM> a �crit dans le message de
> > news:etberCQ4DHA.2432@TK2MSFTNGP09.phx.gbl...
> > > Spot on
> > >
> > > The thread will terminate just like any other.
> > > And just like any outher will not compleetly release all the alocated
> > > resorces untill the handle returnd by CreateThread is closed.
> > >
> > > news:e82EZyO4DHA.1868@TK2MSFTNGP10.phx.gbl...
> > > > "mosfet" <tricubes@wanadoo.fr> wrote in message
> > > > news:buohsc$511$1@news-reader2.wanadoo.fr...
> > > > > when I use a thread in C++ I generally do this :
> > > > >
> > > > > DWORD dwThreadId = 0;
> > > > >  m_hRecordThread = ::CreateThread(0,0,RecordingThread,
> > > > > (LPVOID)this,0,&dwThreadId);
> > > > >
> > > > > DWORD WINAPI CEnrollBioDlg::RecordingThread(LPVOID lpArg)
> > > > > {
> > > > >  CEnrollBioDlg* pThis = reinterpret_cast<CEnrollBioDlg*>(lpArg);
> > > > >  return pThis->RecordingThread();
> > > > > }
> > > > >
> > > > > DWORD CEnrollBioDlg::RecordingThread(void)
> > > > > {
> > > > > blabla ........
> > > > >
> > > > > return 0; // THREAD returns
> > > > > }
> > > > >
> > > > > What I am wondering is when the thread returns does it still exist
> and
> > > in
> > > > > this case do i need to explicitly call a function to terminate it
or
> > is
> > > ok
> > > > > like this.
> > > > > And if I need to kill it from where do I need to do it ?
> > > >
> > > > I'm no thread expert, but I think you still need to call CloseHandle
> the
> > > > CreateThread return value. Otherwise and FWIW, see this CreateThread
> > > > discussion in Jeff Richter's July 1999 MSJ column...
> > > >
> > > > http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
> > > >
> > > > -- 
> > > > Jeff Partch [VC++ MVP]
> > > >
> > > >
> > >
> > >
> >
> >
>
>


0
tricubes (61)
1/23/2004 1:19:53 PM
Reply:

Similar Artilces: