Thread.Join方法的用途:在继续执行标准的 COM 和 SendMessage 消息泵处理期间,阻塞调用线程,直到某个线程终止为止。
实例:
Imports System.Threading
Public Class IsThreadPool
Shared Sub Main() Dim autoEvent As New AutoResetEvent(False)
Dim regularThread As New Thread(AddressOf ThreadMethod) regularThread.Start() ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, autoEvent)
' 等待前台线程结束. regularThread.Join()
' 等待后台线程结束. autoEvent.WaitOne() End Sub
Shared Sub ThreadMethod() Console.WriteLine("ThreadOne, executing ThreadMethod, " & _ "is from the thread pool? {0}", _ Thread.CurrentThread.IsThreadPoolThread) End Sub
Shared Sub WorkMethod(stateInfo As Object) Console.WriteLine("ThreadTwo, executing WorkMethod, " & _ "is from the thread pool? {0}", _ Thread.CurrentThread.IsThreadPoolThread)
' 标识线程已经完成
DirectCast(stateInfo, AutoResetEvent).Set() End Sub
End Class
|