2012-11-03 118 views
0

我有一個包含以下vb.net停止的代碼,直到一個線程執行finshed

WebUpdateThread = New System.Threading.Thread(AddressOf generatecids) 
    WebUpdateThread.SetApartmentState(ApartmentState.STA) 
    WebUpdateThread.Start() 

    'after starting the thread i have some code that i want to run when the thread finshes its work which takes about 3-4 hours 
'if i put for example 
MsgBox("something") 

消息框儘快顯示功能的線程啓動,我如何讓它等到線程是否被沖洗?

有沒有比檢測線程狀態的infitine while循環更充分的東西?

+0

看到這個類似的線程http://stackoverflow.com/questions/ 5551258/c-net-how-to-alert-program-that-thread-is-finished-event-driven – K3N

回答

3

可以使用BackGroundWorker Class,它是專爲只是這種情況下,還可以實現進度指示讓你的用戶不知道什麼是使用ProgressChanged事件發生:

例子:

Imports System.Threading 
Imports System.ComponentModel 


Public Class Form1 
    Dim WebUpdateWorker As BackgroundWorker 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     WebUpdateWorker = New BackgroundWorker 
     AddHandler WebUpdateWorker.DoWork, AddressOf DoWork 
     AddHandler WebUpdateWorker.RunWorkerCompleted, AddressOf WorkFinished 
     WebUpdateWorker.RunWorkerAsync() 

    End Sub 

    Public Sub generatecids() 
     System.Threading.Thread.Sleep(20000) 
    End Sub 

    Private Sub DoWork(sender As Object, e As DoWorkEventArgs) 
     generatecids() 
    End Sub 

    Private Sub WorkFinished(sender As Object, e As RunWorkerCompletedEventArgs) 
     MsgBox("something") 
    End Sub 

End Class 
1
Please can try as below exapmle : 
============================ 
Friend Class StateObj 
    Friend StrArg As String 
    Friend IntArg As Integer 
    Friend RetVal As String 
End Class 

Sub ThreadPoolTest() 
    Dim TPool As System.Threading.ThreadPool 
    Dim StObj1 As New StateObj() 
    Dim StObj2 As New StateObj() 
    ' Set some fields that act like parameters in the state object. 
    StObj1.IntArg = 10 
    StObj1.StrArg = "Some string" 
    StObj2.IntArg = 100 
    StObj2.StrArg = "Some other string" 
    ' Queue a task 
    TPool.QueueUserWorkItem(New System.Threading.WaitCallback _ 
          (AddressOf SomeOtherTask), StObj1) 
    ' Queue another task 
    TPool.QueueUserWorkItem(New System.Threading.WaitCallback _ 
          (AddressOf AnotherTask), StObj2) 
End Sub 

Sub SomeOtherTask(ByVal StateObj As Object) 
    ' Use the state object fields as arguments. 
    Dim StObj As StateObj 
    StObj = CType(StateObj, StateObj) ' Cast to correct type. 
    MsgBox("StrArg contains the string " & StObj.StrArg) 
    MsgBox("IntArg contains the number " & CStr(StObj.IntArg)) 
    ' Use a field as a return value. 
    StObj.RetVal = "Return Value from SomeOtherTask" 
End Sub 

Sub AnotherTask(ByVal StateObj As Object) 
    ' Use the state object fields as arguments. 
    ' The state object is passed as an Object. 
    ' Casting it to its specific type makes it easier to use. 
    Dim StObj As StateObj 
    StObj = CType(StateObj, StateObj) 
    MsgBox("StrArg contains the String " & StObj.StrArg) 
    MsgBox("IntArg contains the number " & CStr(StObj.IntArg)) 
    ' Use a field as a return value. 
    StObj.RetVal = "Return Value from AnotherTask" 
End Sub 
1

其他的答案是一個更好的解決方案完全,但要回答這個問題問,只是你MsgBox前添加WebUpdateThread.Join和代碼將等待線程繼續之前完成。

正如我所說的,其他的答案是更好,因爲與你更自由地做其它事情,比如重新繪製表格等

+0

當我添加WebUpdateThread.Join然後我的UI掛起 – user1570048

+0

是的,就像我在我的回答中所說的,回答問題。其他答案正在解決你的真正問題。 –