Threads - BackgroundWorker
Added in .NET 2.0
The BackgroundWorker class uses Threads.
The BackgroundWorker class provides a built-in wrapper for a worker thread.
Communication between the main thread and the worker thread is asynchronous.
The BackgroundWorker class allow you to have a responsive UI while you retrieve your data.
link - learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker
|RunWorkerAsync||This methods starts the background worker.|
|ReportProgress||This method can be called from within the DoWork subroutine as a way of reporting progress directly to your application.|
|ProgressChanged||This event fires as a consequence of calling the ReportProgress method from inside your DoWork subroutine.|
Creating - Design Time
Create a BackgroundWorker and listen for the events that report progress and signal when the operation has finished.
If you drag it onto a form it will appear in the Components Tray underneath the form.
Creating - Run Time
You can create a BackgroundWorker programmatically or you can drag it onto a form from the Components tab of the Toolbox.
_MyWorker = new System.ComponentModel.BackgroundWorker
_MyWorker.DoWork += _MyWorker_DoWork;
_MyWorker.ProgressChanged += _MyWorker_ProgressChanged
_MyWorker.RunWorkerCompleted += _MyWorker_RunWorkerCompleted
This event fires as a consequence of calling the RunWorkerAsync method.
This fires (on a different thread) as soon as the background worker is started.
This should not have any error handling (it should be handled in the RunWorkerCompleted).
The code inside this event cannot communicate directly with the rest of your application as it runs on a different thread.
Private Sub MyWorker_DoWork() Handles MyWorker.DoWork
'This must not reference any controls on the windows form
'Either use local variables or parameters
'this is necessary as the backgroundworker might have been cancelled before it completed
If (MyWorker.CancellationPending = True) Then Exit Sub
'this is necessary when running a backgroundworker behind a windows form. The form might have been closed while the backgroundworker is still running
If (Me.IsDisposed = False) Then
Async call inside background worker ?
we can kill background worker but it doesn't kill async call ??
Remember that the code inside your DoWork subroutine cannot communicate directly with your main application.
This method in turn fires the ProgressChanged event which is fired on your main thread.
Use the ProgressChanged event to receive notifications of progress updates
Private Sub MyWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles MyWorker.ProgressChanged
progressBar1.Value = e.ProgressPercentage
This event fires once the code in the DoWork subroutine has completed.
This fires on the main thread and can therefore communicate directly with your application.
This should be used to handle any exceptions (caused by the DoEvents) because it is on the main thread.
This event also fires when the backgroundworker is cancelled (ie the CancelAsync method is called)
Private Sub MyWorker_RunWorkerCompleted() Handles MyWorker.RunWorkerCompleted
If (e.Cancelled = True) Then
Call MessageBox("The task was cancelled.")
If e.Error <> Null Then
Call MessageBox("There was an error: " & CType(e.Error, Exception).ToString)
If the operation requires a parameter, call RunWorkerAsyn with this parameter
This parameter can then be extracted from the DoWorkEventArgs argument passed to the DoWork event.
BackgroundWorker - cancelling an async - csharp-examples.net/cancel-asynchronous-method/
VB includes a SyncLock statement that can be used as a gatekeeper.
You can use a button to cancel the backgroundworker
Private Sub btnCancel_Click(sender As , e As EventArgs)
Private Sub btnCancel_Click()
If MyWorker.WorkerSupportsCancellation = True Then
Calling the CancelAsync method will fire the RunWorkerCompleted event although this should not be used as a method of identifying if the backgroundworker has completed.
It is quite possible that the RunWorkerCompleted event is fired some time after the CancelAsync method was called
You should check the CancellationPending property to tell if the backgroundworker has been cancelled.
Marshalling / Blocking
Marshalling is the name when you switch from a secondary thread to the primary UI thread.
It is the act of calling Invoke or BeginInvoke on a from that switches the flow of execution from the secondary thread to the primary thread.
Invoke - This is a blocking call
BeginInvoke - This is not a blocking call
Non blocking calls are the preferred method.
SS - cross thread operation not valid - accessed from a thread other than the thread it was created on.
Passing a Parameter
It is possible to pass a parameter to a background worker.
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext