0

我有一个包含以下内容的函数

  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")

消息框在线程启动后立即显示,我如何让它等到线程完成?

有没有比检查线程状态的无限while循环更充分的东西?

4

3 回答 3

3

您可以使用BackGroundWorker 类,它专为这种情况而设计,您还可以实现进度指示,这样您的用户就不会想知道使用 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
于 2012-11-03T06:35:09.983 回答
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
于 2012-11-03T06:25:38.620 回答
1

其他答案是更好的完整解决方案,但要回答所提出的问题,只需WebUpdateThread.Join在您之前添加MsgBox,代码将等待线程完成后再继续。

正如我所说,其他答案更好,因为有了这些答案,您可以更自由地做其他事情,例如重新绘制表格等。

于 2012-11-04T06:34:26.000 回答