这是一个非常基本的示例;main 方法只是创建两个线程并启动它们。第一个线程等待 10 秒,然后设置_WaitHandle_FirstThreadDone。第二个线程只是等待_WaitHandle_FirstThreadDone设置后再继续。两个线程同时启动,但第二个线程将等待第一个线程设置_WaitHandle_FirstThreadDone后再继续。
有关详细信息,请参阅: System.Threading.AutoResetEvent。
Module Module1
Private _WaitHandle_FirstThreadDone As New System.Threading.AutoResetEvent(False)
Sub Main()
Console.WriteLine("Main Started")
Dim t1 As New Threading.Thread(AddressOf Thread1)
Dim t2 As New Threading.Thread(AddressOf thread2)
t1.Start()
t2.Start()
Console.WriteLine("Main Stopped")
Console.ReadKey()
End Sub
Private Sub Thread1()
Console.WriteLine("Thread1 Started")
Threading.Thread.Sleep(10000)
_WaitHandle_FirstThreadDone.Set()
Console.WriteLine("Thread1 Stopped")
End Sub
Private Sub thread2()
Console.WriteLine("Thread2 Started")
_WaitHandle_FirstThreadDone.WaitOne()
Console.WriteLine("Thread2 Stopped")
End Sub
End Module