这是休眠线程的模式以及如何“中止”它。请注意,.WaitOne 可以采用类似于 Thread.Sleep 的参数。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'start a thread
If IsNothing(somethrd) Then
somethrd = New Threading.Thread(AddressOf test)
somethrd.IsBackground = True
somethrd.Start()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'stop
stopsomethrd = True
somethrdwait.Set()
somethrd.Join()
Debug.WriteLine("end")
End Sub
Dim somethrd As Threading.Thread
Dim somethrdwait As New Threading.AutoResetEvent(False)
Dim stopsomethrd As Boolean
Private Sub test()
Do
'your code
somethrdwait.WaitOne() 'suspend thread
If stopsomethrd Then Exit Do
Loop
End Sub