Executing a task with the background worker.
Double Click on the BackgroundWorker
control from the Toolbox
This is how the BackgroundWorker appears after adding it.
Double click on the added control to get the BackgroundWorker1_DoWork
event and add the code to be executed when the BackgroundWorker is called. Something like this:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Do the time consuming background task here
End Sub
Calling the BackgroundWorker to perform the task can be done at any event like Button_Click
, Textbox_TextChanged
, etc. as follows:
BackgroundWorker1.RunWorkerAsync()
Modify the RunWorkerCompleted
event to capture the task finished event of the BackgroundWorker as follows:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("Done")
End Sub
This will display a message box saying Done
when the worker finishes the task assigned to it.