2012-11-03 92 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

是的,就像我在我的回答中所说的,回答问题。其他答案正在解决你的真正问题。 –