|
-
June 7th, 1999, 03:03 PM
#1
Thread !?!?! Please some help needed here...
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 !
-
June 8th, 1999, 12:47 AM
#2
Re: Thread !?!?! Please some help needed here...
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
-
June 8th, 1999, 03:07 PM
#3
Re: Thread !?!?! Please some help needed here...
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 
-
June 8th, 1999, 03:36 PM
#4
Re: Thread !?!?! Please some help needed here...
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|