Advantages of the BackgroundWorker Class

I work with worker threads using .NET Thread class, both in C++/CLI and C#. I use them to talk with hardware devices and for background calculations. To notify UI I use events or asynchronous delegates, prefering asynchronous notifications to prevent deadlocks. Every worker thread has associated EventWaitHandle instance which I set when I need to stop this thread. So, I work exactly by the same way as in Win32 programming, this gives good results and I am quite happy with this.
The question: does BackgroundWorker Class have any advantages, is there reason to use this class instead of "plain" Thread class Some features which are implemented better and can improve my program


Answer this question

Advantages of the BackgroundWorker Class

  • ch.nagaraj

    BackgroundWorker offers a simplified programming model.

    It uses threads from the CLR thread pool.

    It marshals its events on the UI thread.

    It is most useful in UI applications.

    You might want to take a look at "Asynchronous Programming Design Patterns" in MSDN.


  • Gary Hall

    I wonder why first reply is marked as answer if I didn't do this Anyway, I prefer to decide myself what is answer to my question.
  • Taras Naumtsev

    Lucian is correct. The biggest advantage (in my opinion) is the marshaling to the UI thread. When you create a BackgroundWorker object in the Form you want it to communicate with, it automatically stores the context in which correct UI marshaling should occur. This means you can avoid checking the InvokeRequired property in the ProgressChanged and RunWorkerCompleted event handlers (assuming they're on the Form object processed from the main thread that created the BackgroundWorker object)... which means avoiding having to go through the hoops to call Invoke/BeginInvoke in these event handlers.

    That would be the only advantage over using a thread pool thread. e.g. ThreadPool.QueueUserWorkItem(...).



  • Advantages of the BackgroundWorker Class