Take the 2-minute tour ×

I can remember that many years ago (in 2005) I was using BackgroundWorker in my code without using a visual component for it, but I can't remember how (unfortunately I am very forgetful and forget everything very soon after I stop using it). Perhaps I was extending BackgroundWorker class. Can you link to a good example of using BackgroundWorker this way?

share|improve this question
quick search -> msdn: msdn.microsoft.com/en-us/library/… (look for FibonacciForm) –  manji Jun 16 '11 at 0:25
It's just a class like any other, you can create an instance in your code then work with it. This question basically has your answer: stackoverflow.com/questions/3767827 –  Alastair Pitts Jun 16 '11 at 0:26
The way it works can forget quite easily (specially which methods works in main thread and which aren't) but easy to refresh in a few mins. –  CharithJ Jun 16 '11 at 0:40
@manji, that example is exactly what Ivan said he wants to avoid, dropping the worker onto a form. I realize for many people this is obvious, but for those who have are self taught or even worse, taught poorly by supervisors, using some components directly without dropping them onto a form is a new concept. This question is clear and doesn't deserve all the down votes. –  Samuel Neff Jun 16 '11 at 2:06
@Samuel Neff : Exactly! +1 –  CharithJ Jun 16 '11 at 2:13
show 2 more comments

1 Answer

up vote 20 down vote accepted

This article explains everything you need clearly.

Here are the minimum steps in using BackgroundWorker:

  1. Instantiate BackgroundWorker and handle the DoWork event.
  2. Call RunWorkerAsync, optionally with an object argument.

This then sets it in motion. Any argument passed to RunWorkerAsync will be forwarded to DoWork’s event handler, via the event argument’s Argument property. Here’s an example:

class Program
  static BackgroundWorker _bw = new BackgroundWorker();

  static void Main()
    _bw.DoWork += bw_DoWork;
    _bw.RunWorkerAsync ("Message to worker");

  static void bw_DoWork (object sender, DoWorkEventArgs e)
    // This is called on the worker thread
    Console.WriteLine (e.Argument);        // writes "Message to worker"
    // Perform time-consuming task...
share|improve this answer
great link, but the answer is better with appropriate quotes and code. –  Samuel Neff Jun 16 '11 at 2:08
@Samuel Neff : Thanks! –  CharithJ Jun 16 '11 at 2:12
@samuel-neff, thanks! –  Ivan Jun 16 '11 at 13:20
Helped me while searching for a simple method for BG worker. –  Vikneshwar Nov 7 '13 at 16:43
add comment

Your Answer


