How to wake up a sleeping thread?

  • Thread starter Thread starter Muscha
  • Start date Start date
M

Muscha

Hello,

I have a thread that in the middle of the execution I did Thread.Sleep().
How do I tell this thread to abort it sleep and continues? Is there a way?

thanks,

/m
 
Use Suspend() instead of Sleep(), then use Resume().

However Suspend() does no take the parameter how long should I suspend it
for. In this case I need to setup another thread just to resume this
suspended thread.

/m
 
I suggest that you look at the WaitHandle class, where you can use, perhaps,
WaitOne( int millisecondsTimeout, bool exitContext )
Then to interrupt it before the timeout, you'd use the Set method in one of
the derived, instantiable classes, such as AutoResetEvent.
You can check the return value of WaitOne to determine if it was a timeout
or an event.

Hope this helps,
Chris R.
 
Hi,

You might use Monitor instead.
From the thread call Monitor.Wait (object that becomes locked, TimeSpan)
From the other thread call Monitor.Pulse to awake the waiting thread.
 
Back
Top