 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Star Guest
|
Posted: Thu Mar 03, 2005 10:45 pm Post subject: Wake up a thread after calling Sleep() funtion |
|
|
Hi,
Is there anyway to do this?
If I have a thread, and inside the Execute method I do
Sleep(30000)
Can I wake up that thread from the main thread? I tried with Resume but it
didn't
work.
Thanks
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Mar 03, 2005 11:17 pm Post subject: Re: Wake up a thread after calling Sleep() funtion |
|
|
"Star" <nospam (AT) nospam (DOT) com> wrote
| Quote: | If I have a thread, and inside the Execute method I do
Sleep(30000)
Can I wake up that thread from the main thread?
|
No. You cannot interrupt Sleep(). It does not return until the specified
interval has elapsed. If you need to wake up a sleeping thread, then you
need to use a smaller interval and then loop the Sleep(), ie:
for(DWORD dw = 0; dw < 30; ++dw)
{
Sleep(1000);
if( SomeCondition )
break;
}
Otherwise, use CreateEvent() and WaitForSingleObject() instead:
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
//...
WaitForSingleObject(hEvent, 30000);
//...
CloseHandle(hEvent);
Other threads can call SetEvent() using the HANDLE in order to cause
WaitForSingleObject() to exit sooner than the timeout.
Gambit
|
|
| Back to top |
|
 |
Star Guest
|
Posted: Fri Mar 04, 2005 2:47 am Post subject: Re: Wake up a thread after calling Sleep() funtion |
|
|
Thanks a lot for the examples, Remy. I think I will use CreateEvent()
|
|
| Back to top |
|
 |
Sam S. Firouz Guest
|
Posted: Tue Mar 08, 2005 7:05 pm Post subject: Re: Wake up a thread after calling Sleep() funtion |
|
|
You can also check out TEvent. The nice people at Borland have wraped
CreateEvent and all its complex functions into a simple class.
Sam
"Star" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Thanks a lot for the examples, Remy. I think I will use CreateEvent()
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|