首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >vb.net暂停代码的执行,直到线程完成

vb.net暂停代码的执行,直到线程完成
EN

Stack Overflow用户
提问于 2012-11-03 14:14:12
回答 3查看 1.4K关注 0票数 0

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

代码语言:javascript
运行
复制
  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循环更充分的检查线程状态的方法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-03 14:35:10

你可以使用BackGroundWorker Class,它就是为这个场景设计的,你也可以实现进度指示,这样你的用户就不会通过使用ProgressChanged事件来想知道发生了什么:

示例:

代码语言:javascript
运行
复制
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
票数 3
EN

Stack Overflow用户

发布于 2012-11-03 14:25:39

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2012-11-04 14:34:26

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

正如我所说的,其他答案更好,因为使用这些答案,您可以更自由地做其他事情,如重新绘制窗体等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13206558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档