What is the difference in using:
BackgroundWorker1 or System.Threading.Thread
Printable View
What is the difference in using:
BackgroundWorker1 or System.Threading.Thread
The BackgroundWorker is a component that wraps a thread. It has the advantage that it provides a couple of events that, when raised, are raised on the UI thread rather than in the background thread. Other than that, there really isn't much difference at all, as one is a wrapper built on the other.
Here is a thread where a guy asks something similar. There are more details about threading in general there.
Parallelism is about having multiple tasks performed simultaneously. Parallelism is achieved using multi-threading and multi-threading in .NET is achieved using the Thread class. Each Thread represents an area within which statements can be executed in serial so multiple Threads allows multiple instructions to be executed in parallel.
The BackgroundWorker is several layers of abstraction higher than the Thread class. The BackgroundWorker makes use of the ThreadPool class and that makes use of the Thread class. The BackgroundWorker was designed specifically for use in scenarios where you have a Windows GUI application within which you want to perform some long-running task and update the UI during and/or after that task. If you were to perform that task on the UI thread then your GUI would freeze. You could achieve the same thing with the Thread class explicitly but updating the UI is fiddly in that case. The BackgroundWorker enables you to perform a task in the background and update the UI by simply calling methods and handling events, which we've all done before. The one rule to remember is that you can never access a control within the DoWork event handler.
For a BackgroundWorker example, follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker. To learn how to update the UI from a background thread without using a BackgroundWorker, check out my CodeBank thread on Accessing Controls From Worker Threads.