27

What is the difference between creating a thead using BackgroundWorker and creating a thread using System.Threading.Thread?

Icemanind
  • 47,519
  • 50
  • 171
  • 296

2 Answers2

38

The BackgroundWorker class basically abstracts the Thread creation and monitoring process, and gives you an event-driven API to report the progress of the operation (ProgressChanged) and determine when your operation is finished (RunWorkerCompleted)...

One of the most common uses for it is to keep a Windows GUI responsive while a long-running process executes in the background. So basically, its just a wrapper for System.Threading.Thread designed to make background threading a little simpler (as the name implies!)

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • So basically, its just a wrapper for System.Threading.Thread designed to make threading a little simpler? – Icemanind Oct 24 '09 at 19:48
  • 1
    http://stackoverflow.com/questions/1506838/backgroundworker-vs-background-thread/1507337#1507337 – Matt Davis Oct 25 '09 at 02:33
  • @MattDavis Great link, it would probably be more meaningful if there was a description or something more than JUST the link. – Randolph Jun 25 '13 at 20:01
  • I am trying to reconcile your comment: "while a long-running process executes in the background" with Hans Passant's [comment](http://stackoverflow.com/a/17009983/848344) where he said: "It is important that you use a thread pool [BackgroundWorker] thread only when the work it does is limited, ideally not taking more than half a second.". What gives? – Dan W May 09 '16 at 08:08
19

BackgroundWorker is actually a wrapper for asynchronous thread invocation via delegates - using reflector one can see it calls the begin/end invoke methods accordingly. This differs from a System.Threading.Thread in that it uses the threadpool as opposed to starting up a brand new thread.

The main reason for using background worker is that it plugs in nicely with windows forms applications.

CDspace
  • 2,639
  • 18
  • 30
  • 36
Mike Tours
  • 772
  • 2
  • 6
  • 13
  • 4
    +1 for accurately capturing the fact that background workers use the thread pool. In addition it is useful for people ot know that the BackgroundWorker is not a good choice if you need to call an STA Com object as the apartment cannot be set – Steve Sheldon Oct 13 '10 at 09:25