Start a thread that is sleeping?

  • Thread starter Thread starter Fredrik Melin
  • Start date Start date
F

Fredrik Melin

If I use Threading.Thread.Sleep is there a way to cancel that sleep from
another thread?

Regards
Fredrik
 
Hi Frederik,

As far as I can see you can only let it sleep some time, so you have to let
it sleep.

But I think you can set somewhere a shared boolean from any thread when you
have build a loop in your thread like this.

do while shared_boolean_for_this_thread
thread.sleep(50)
loop

I thought you can set that boolean to false in another thread.
I thought I did somethings like that but not this.

So just a thought never tested.

Cor
 
Fredrik,
Not per se.

If you need the ability you should use a WaitHandle, such as AutoResetEvent
or ManualResetEvent.

Then instead of using Threading.Thread.Sleep you would use
Threading.WaitHandle.WaitOne with a timeout value. The other thread can use
*ResetEvent.Set to cause the first thread to wake up. With the timeout on
WaitOne the first thread will automatically wake up if another thread does
not call Set.

Dim event As New AutoResetEvent

Public Sub Tread1()
If event.WaitOne(2000, False) Then
' event.Set was called
Else
' event timed out
End if
End Sub

Public Sub Thread2()
event.Set() ' wake up thread1
End Sub

Hope this helps
Jay
 
Back
Top