Click to See Complete Forum and Search --> : Thread !?!?! Please some help needed here...
Luc B.
June 7th, 1999, 03:03 PM
Hello to all !
My question is simple : Can I use the API function WaitForSingleObject to wait for a thread when that thread was created with AfxBeginThread ???
Here is a sample of my code i'm using to create the thread :
pNewThread = AfxBeginThread(Thread_CheckFile, (LPVOID)&pParam);
pNewThread->m_bAutoDelete = FALSE;
dwRetVal = WaitForSingleObject(pNewThread->m_hThread, UNE_MINUTE);
Any help will be greatly appreciated !
Roland Seibert
June 8th, 1999, 12:47 AM
No. If you use AfxBeginThread the thread is created and the pointer pNewThread is valid. This means the thread exists! So the WaitForSingleObject doesn't wait, because the thread exists whether or not the thread is being executed at this time.
To solve your problem use a CEvent Object (initially not signaled and accessable byboth threads) and wait for the event being signaled. The new thread should call CEvent::SetEvent() at starttime to make it signaled.
Hope this helps
Luc B.
June 8th, 1999, 03:07 PM
Ok... I tried to use the CEvent object but it doesn't seem to work.
Here is what I do :
Declared a global pointer of CEvent type -> CEvent *pEvent;
In my function which creates the thread I instanciate it as follows : pEvent = new CEvent(FALSE);
Then I call AfxBeginThread();
In the function of the worker thread I call pEvent->SetEvent().
My problem is that the calling thread doesn't wait for the worker thread to finish before continuing execution.
How can I make the calling thread wait for the end of the execution of the working before continuing the execution of the program ?!?!?!
Thanx in advance :)
Ignore that stuff about CEvent. Its not necessary and misleading/incorrect. You were on the right track with ::WaitForSingleObject(). If you want the thread that is calling the wait function to block untill the other thread is completed, specify a dwTimeout Parameter of INFINITE. It doesn't make any difference if you used CreateThread, _beginthread, or AfxBeginThread.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.