Juky,
As Herfried suggests I would use a ManualResetEven t.
Here is a an example I posted a few weeks ago of a Worker class using a
ManualResetEven t to control suspending & resuming.
Public Class Worker
Public Delegate Sub Work()
Private ReadOnly m_work As Work
Private ReadOnly m_arg As Object
Private ReadOnly m_event As ManualResetEven t
Private ReadOnly m_thread As Thread
<ThreadStatic() > _
Private Shared m_current As Worker
Public Sub New(ByVal work As Work, ByVal arg As Object, ByVal name
As String)
m_work = work
m_arg = arg
m_event = New ManualResetEven t(True)
m_thread = New Thread(AddressO f Start)
m_thread.Name = name
m_thread.IsBack ground = True
m_thread.Start( )
End Sub
Private Sub Start()
m_current = Me
m_work.Invoke()
End Sub
Public Shared ReadOnly Property CurrentWorker() As Worker
Get
Return m_current
End Get
End Property
Public ReadOnly Property Arg() As Object
Get
Return m_arg
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return m_thread.Name
End Get
End Property
Public Sub Suspend()
If Thread.CurrentT hread Is m_thread Then
Throw New InvalidOperatio nException("Sus pend should not be
called from the worker thread!")
End If
m_event.Reset()
End Sub
Public Sub [Resume]()
If Thread.CurrentT hread Is m_thread Then
Throw New InvalidOperatio nException("Res ume should not be
called from the worker thread!")
End If
m_event.Set()
End Sub
Public Sub WaitForTerminat ion()
If Thread.CurrentT hread Is m_thread Then
Throw New InvalidOperatio nException("Wai tForTermination
should not be called from the worker thread!")
End If
m_thread.Join()
End Sub
Public Sub CheckSuspend()
If Not Thread.CurrentT hread Is m_thread Then
Throw New InvalidOperatio nException("Che ckSuspend should
only be called from the worker thread!")
End If
m_event.WaitOne ()
End Sub
End Class
To see how it works try something like:
Private Sub Work()
For index As Integer = 1 To 50
Worker.CurrentW orker.CheckSusp end()
Debug.WriteLine (index, Worker.CurrentW orker.Name)
Dim value As Double = DirectCast(Work er.CurrentWorke r.Arg,
Double)
Thread.Sleep(Ti meSpan.FromSeco nds(value))
Next
End Sub
Public Sub Main()
Dim worker1 As New Worker(AddressO f Work, 0.25, "worker1")
Dim worker2 As New Worker(AddressO f Work, 0.5, "worker2")
Dim worker3 As New Worker(AddressO f Work, 0.75, "worker3")
Debug.WriteLine ("Pausing 5 seconds", "Main")
Thread.Sleep(Ti meSpan.FromSeco nds(5))
worker1.Suspend ()
Debug.WriteLine ("Pausing 5 seconds", "Main")
Thread.Sleep(Ti meSpan.FromSeco nds(5))
worker2.Suspend ()
Debug.WriteLine ("Pausing 5 seconds", "Main")
Thread.Sleep(Ti meSpan.FromSeco nds(5))
worker3.Suspend ()
Debug.WriteLine ("Pausing 1 seconds", "Main")
Thread.Sleep(Ti meSpan.FromSeco nds(1))
worker1.Resume( )
worker2.Resume( )
worker3.Resume( )
Debug.WriteLine ("Waiting for Termination", "Main")
worker1.WaitFor Termination()
worker2.WaitFor Termination()
worker3.WaitFor Termination()
End Sub
The above sample was posted in a thread titled "Technique for Pausing Worker
Thread" in this newsgroup between 21 Jan 2005 & 23 Jan 2005.
Hope this helps
Jay
"juky" <ju*******@yaho o.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi all,
I have 3 running threads chencking for something in my application.
Based on that I need to stop some of them safely wait for something
else and restart. Sleep is not good in my case because i don't know how
much to wait. How and from where I can put a thread in pause an restart
it?
Thank you
Juky
Sub main ()
....
t1 = New Threading.Threa d(AddressOf Check1)
t1.Start()
t2 = New Threading.Threa d(AddressOf Check2)
t2.Start()
t3 = New Threading.Threa d(AddressOf Check3)
t3.Start()
end sub
sub Check1()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub
sub Check2()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub
sub Check3()
while true
"IF CHECK=TRUE THEN I HAVET TO SUSPEND THIS THREAD
end while
end Sub