Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync(ParameterOptionalHere)
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' grab the parameter if used:
Dim x = e.Argument ' cast to whatever you need
Foo(x)
End Sub
Private Sub Foo()
' ...your existing sub...
' Use ReportProgress() if you need to update the GUI during the operation:
BackgroundWorker1.ReportProgress(0, ANY_OBJECT_HERE)
' first parameter is typically 0 to 100 for a progressbar
' second parameter can be used to passy ANYTHING
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
' ...update the GUI from this event...
e.ProgressPercentage ' first parameter passed to ReportProgress()
e.UserState ' second parameter passed to ReportProgress() --> cast it with CType()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
' ...this fires when the backgroundworker operation is complete...
End Sub
End Class
|