A BackgroundWorker is a ready to use class in WinForms allowing you to execute tasks on background threads which avoids freezing the UI and in addition to this allows you to easily marshal the execution of the success callback on the main thread which gives you the possibility to update the user interface with the ...
BackgroundWorker is explicitly labeled as obsolete in . NET 4.5: in the book By Joseph Albahari, Ben Albahari "C# 5.0 in a Nutshell: The Definitive Reference"
CsharpServer Side ProgrammingProgramming. As the name suggests the Background Worker Class allows you to set a thread continuously running in the background and communicating with the main thread whenever required. BackgroundWorker makes the implementation of threads in Windows Forms.
The BackgroundWorker class exposes the DoWork event, to which your worker thread is attached through a DoWork event handler. The DoWork event handler takes a DoWorkEventArgs parameter, which has an Argument property.
Some of my thoughts...
From my understanding of your question, you are using a BackgroundWorker
as a standard Thread.
The reason why BackgroundWorker
is recommended for things that you don't want to tie up the UI thread is because it exposes some nice events when doing Win Forms development.
Events like RunWorkerCompleted
to signal when the thread has completed what it needed to do, and the ProgressChanged
event to update the GUI on the threads progress.
So if you aren't making use of these, I don't see any harm in using a standard Thread for what you need to do.
Pretty much what Matt Davis said, with the following additional points:
For me the main differentiator with BackgroundWorker
is the automatic marshalling of the completed event via the SynchronizationContext
. In a UI context this means the completed event fires on the UI thread, and so can be used to update UI. This is a major differentiator if you are using the BackgroundWorker
in a UI context.
Tasks executed via the ThreadPool
cannot be easily cancelled (this includes ThreadPool
. QueueUserWorkItem
and delegates execute asyncronously). So whilst it avoids the overhead of thread spinup, if you need cancellation either use a BackgroundWorker
or (more likely outside of the UI) spin up a thread and keep a reference to it so you can call Abort()
.
Also you are tying up a threadpool thread for the lifetime of the background worker, which may be of concern as there are only a finite number of them. I would say that if you are only ever creating the thread once for your app (and not using any of the features of background worker) then use a thread, rather than a backgroundworker/threadpool thread.
You know, sometimes it's just easier to work with a BackgroundWorker regardless of if you're using Windows Forms, WPF or whatever technology. The neat part about these guys is you get threading without having to worry too much about where you're thread is executing, which is great for simple tasks.
Before using a BackgroundWorker
consider first if you wish to cancel a thread (closing app, user cancellation) then you need to decide if your thread should check for cancellations or if it should be thrust upon the execution itself.
BackgroundWorker.CancelAsync()
will set CancellationPending
to true
but won't do anything more, it's then the threads responsibility to continually check this, keep in mind also that you could end up with a race condition in this approach where your user cancelled, but the thread completed prior to testing for CancellationPending
.
Thread.Abort()
on the other hand will throw an exception within the thread execution which enforces cancellation of that thread, you must be careful about what might be dangerous if this exception was suddenly raised within the execution though.
Threading needs very careful consideration no matter what the task, for some further reading:
Parallel Programming in the .NET Framework Managed Threading Best Practices
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With