
SetTextCallback d = new SetTextCallback(SetText) It's on a different thread, so use Invoke. Check if this method is running on a different thread // than the thread that created the control. String text = " Written by the background thread." Wait two seconds to simulate some background work // being done. Private Sub bgwLongTask_RunWorkerCompleted(ByVal sender As _ PrgPercentComplete.Value = e.ProgressPercentage Private Sub bgwLongTask_ProgressChanged(ByVal sender As _ If (bgwLongTask.CancellationPending) Then Private Sub bgwLongTask_DoWork(ByVal sender As _ Private Sub btnGo_Click(ByVal sender As System.Object, _īyVal e As System.EventArgs) Handles btnGo.Click ' Use the BackgroundWorker to perform a long task. Refer to the previous list as you look through the following code to see what each piece is supposed to do. The code for this example is relatively straightforward. The e.Cancelled property tells whether the worker finished or was stopped. The UI thread can take action to clean up here. RunWorkerCompleted - This event occurs when the worker's DoEvent event handler ends.The UI thread can update labels, progress bars, and so forth to report progess to the user. ProgressChanged - This event occurs when the worker calls ReportProgress.ReportProgress - The worker can call this method to pass progress information back to the UI thread.This sets the worker's CancellationPending to true. CancelAsync - The UI thread can call this method to tell worker to stop.If the worker is stopped, it should set the event's e.Cancel property to true. It should periodically check its CancellationPending property to see if the UI thread is trying to stop it. DoWork - The DoWork event is where the worker should do its work.RunWorkerAsync - The UI thread (the main program) should call this method to start the worker.The key pieces to the BackgroundWorker are: The BackgroundWorker component provides a slightly easier way to perform a background task. Unfortunately using threads directly can be confusing. The examples in the Multithreading section let you perform more than one task at the same time. Multi-threading, threads, threading, multithreading, BackgroundWorker, background worker, Visual Basic. This example shows how to use a BackgroundWorker to perform a task in the background in Visual Basic. Use a BackgroundWorker to perform a task in the background in Visual Basic. VB Helper: HowTo: Use a BackgroundWorker to perform a task in the background in Visual Basic.
