如何使线程等到另一个线程完成?

use*_*951 1 vb.net multithreading

Static lock As Object

SyncLock lock
    TraverSingweb.TraverSingWeb.WebInvoke(Sub() TraverSingweb.TraverSingWeb.putHtmlIntoWebBrowser(theenchancedwinclient)) 'This quick function need to finish before we continue
End SyncLock

SyncLock lock
    'SuperGlobal.lockMeFirst(AddressOf SuperGlobal.doNothing) ' donothing
End SyncLock
Run Code Online (Sandbox Code Playgroud)

这就是我目前在vb.net中的做法.这是一种模式吗?

Ran*_*son 7

这是一个非常基本的例子; 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
Run Code Online (Sandbox Code Playgroud)