BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Wake up a thread after calling Sleep() funtion

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Star
Guest





PostPosted: Thu Mar 03, 2005 10:45 pm    Post subject: Wake up a thread after calling Sleep() funtion Reply with quote



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





PostPosted: Thu Mar 03, 2005 11:17 pm    Post subject: Re: Wake up a thread after calling Sleep() funtion Reply with quote




"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





PostPosted: Fri Mar 04, 2005 2:47 am    Post subject: Re: Wake up a thread after calling Sleep() funtion Reply with quote



Thanks a lot for the examples, Remy. I think I will use CreateEvent()


Back to top
Sam S. Firouz
Guest





PostPosted: Tue Mar 08, 2005 7:05 pm    Post subject: Re: Wake up a thread after calling Sleep() funtion Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.