VB.Net backgroundworker wait for finish

To wait for a BackgroundWorker to finish in VB.NET, you can use the IsBusy property. The IsBusy property returns True if the BackgroundWorker is currently executing a task, and False otherwise.

IsBusy property

To wait for a BackgroundWorker to finish, you can simply loop while the IsBusy property is True. For example, the following code shows how to wait for a BackgroundWorker to finish before continuing:

Dim backgroundWorker As New BackgroundWorker() backgroundWorker.DoWork += AddressOf backgroundWorker_DoWork backgroundWorker.RunWorkerCompleted += AddressOf backgroundWorker_RunWorkerCompleted backgroundWorker.RunWorkerAsync() ' Wait for the BackgroundWorker to finish. While backgroundWorker.IsBusy DoEvents() End While ' Continue with the rest of your code. Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) ' Do something. End Sub Private Sub backgroundWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) ' Do something. End Sub

RunWorkerCompleted event

You can also use the RunWorkerCompleted event to be notified when a BackgroundWorker has finished executing a task. The RunWorkerCompleted event handler is passed a RunWorkerCompletedEventArgs object, which contains information about the task that was completed.

For example, the following code shows how to use the RunWorkerCompleted event to be notified when a BackgroundWorker has finished executing a task:

Dim backgroundWorker As New BackgroundWorker() backgroundWorker.DoWork += AddressOf backgroundWorker_DoWork backgroundWorker.RunWorkerCompleted += AddressOf backgroundWorker_RunWorkerCompleted backgroundWorker.RunWorkerAsync() Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) ' Do something. End Sub Private Sub backgroundWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) If e.Error IsNot Nothing Then ' Handle the error. Else ' The task completed successfully. End If End Sub

Using the IsBusy property or the RunWorkerCompleted event to wait for a BackgroundWorker to finish is the best way to ensure that your code does not continue until the BackgroundWorker has finished executing its task.

VB.Net BackgroundWorker

The BackgroundWorker component is used to perform time-consuming operations in a separate thread, allowing the main UI thread to remain responsive.

Create and Initialize the BackgroundWorker

First, create and initialize a BackgroundWorker instance. You should handle its events, especially the DoWork event, where you define the work to be performed in the background thread.

Dim worker As New BackgroundWorker() AddHandler worker.DoWork, AddressOf DoWorkHandler AddHandler worker.RunWorkerCompleted, AddressOf RunWorkerCompletedHandler ' Start the background worker worker.RunWorkerAsync()

Define the Work to Be Performed

In the DoWork event handler, you define the work that the BackgroundWorker should perform. For example:

Private Sub DoWorkHandler(sender As Object, e As DoWorkEventArgs) ' Perform time-consuming operation here ' This method runs in a separate thread End Sub

Handle the Completion

The RunWorkerCompleted event is raised when the background work is finished. You can use this event to handle the completion of the operation.

Private Sub RunWorkerCompletedHandler(sender As Object, e As RunWorkerCompletedEventArgs) ' Handle the completion of the background work here End Sub

Wait for Completion

To wait for the BackgroundWorker to finish, you can use the RunWorkerAsync method in a blocking manner:

worker.RunWorkerAsync() ' Start the background worker worker.RunWorkerCompleted.WaitOne() ' Wait for the completion

By calling WaitOne, you block the current thread until the background worker completes its operation.

Full Source
Imports System.ComponentModel Dim worker As New BackgroundWorker() AddHandler worker.DoWork, AddressOf DoWorkHandler AddHandler worker.RunWorkerCompleted, AddressOf RunWorkerCompletedHandler worker.RunWorkerAsync() ' Start the background worker worker.RunWorkerCompleted.WaitOne() ' Wait for the completion Private Sub DoWorkHandler(sender As Object, e As DoWorkEventArgs) ' Perform time-consuming operation here ' This method runs in a separate thread End Sub Private Sub RunWorkerCompletedHandler(sender As Object, e As RunWorkerCompletedEventArgs) ' Handle the completion of the background work here End Sub

In this example, we use WaitOne to wait for the BackgroundWorker to complete its work, ensuring that the main thread waits for the background work to finish before proceeding.

Conclusion

You can use the BackgroundWorker component to perform time-consuming operations in a separate thread, allowing the main UI thread to stay responsive. To wait for a BackgroundWorker to finish, you can use the RunWorkerCompleted event or WaitOne method to block the main thread until the background work is complete, ensuring orderly execution of tasks.