I came to know that Thread.Suspend is not a good way to pause a thread indefinitely. Please let me know if other way to achieve same.
Thanks in advance.
I came to know that Thread.Suspend is not a good way to pause a thread indefinitely. Please let me know if other way to achieve same.
Thanks in advance.
A short vb example
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
thrd.IsBackground = True
thrd.Start()
End Sub
Dim thrd As New Threading.Thread(AddressOf somethread)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'pause and resume
'first click pauses, second click resumes
reqpause.Set() 'set event
End Sub
Dim reqpause As New Threading.AutoResetEvent(False)
Private Sub somethread()
Do
'simulate work - your code here
Threading.Thread.Sleep(500)
Debug.WriteLine(DateTime.Now.ToLongTimeString)
'end simulate work
If reqpause.WaitOne(0) Then 'pause requested?
Debug.WriteLine("t-thrd paused")
reqpause.WaitOne() 'wait here for continuation
Debug.WriteLine("t-continue thrd")
End If
Loop
End Sub
Using Suspend
and Resume
is not a good idea. I am not sure why you want to wait forever, but to wait use an EventWaitHandle
to wait.
private EventWaitHandle handle = new AutoResetEvent();
private void WorkerThread()
{
while(true)
{
handle.WaitOne();
}
}
//Forever is a long time
public void StopWaitingForever()
{
handle.Set();
}