user1570048
user1570048

Reputation: 880

vb.net halting the execution of a code until a thread is finished

i have a function that contains the following

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

the message box shows as soon as the thread starts, how do i make it wait until the thread is finshed?

is there something more sufficient than an infitine while loop that checks the thread state?

Upvotes: 0

Views: 1425

Answers (3)

Mark Hurd
Mark Hurd

Reputation: 10931

The other answers are a better full solutions, but to answer the question asked, just add WebUpdateThread.Join before your MsgBox and the code will wait for the thread to finish before continuing.

As I said, the other answers are better because with those you're more free to do other things, like repainting a form, etc.

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54532

You can use the BackGroundWorker Class, it is designed for just this scenario, you also can implement progress indication so your user is not wondering what is happening by using the ProgressChanged Event:

Example:

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

Upvotes: 3

Vikram Jain
Vikram Jain

Reputation: 5588

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

Upvotes: 1

Related Questions