Take the 2-minute tour ×

Let's say I want to sent an int parameter to a background worker, how can this be accomplished?

private void worker_DoWork(object sender, DoWorkEventArgs e) {


I know when this is worker.RunWorkerAsync();, I don't understand how to define in worker_DoWork that it should take an int parameter.

share|improve this question

6 Answers 6

up vote 78 down vote accepted

You start it like this:

int value = ...;
bgw1.RunWorkerAsync(value);  // argument: value,  the int will be boxed

and then

private void worker_DoWork(object sender, DoWorkEventArgs e) 
   int value = (int) e.Argument;   // the 'argument' parameter resurfaces here
share|improve this answer
How can I do two arguments? –  sooprise Jan 26 '11 at 16:45
Or do I send an object full of more than one argument? –  sooprise Jan 26 '11 at 16:46
@soo: Use a helper class or a Tuple<A,B> (C#4+) (Edit: Yes, use an object to pack it all in. See for example DoWorkEventArgs self). –  Henk Holterman Jan 26 '11 at 16:47
Ok, thanks :) !! –  sooprise Jan 26 '11 at 16:49

Even though this is an already answered question, I'd leave another option that IMO is a lot easier to read:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, e) => WorkerDoWork(value, text);

And on the handler method:

private void WorkerDoWork(int value, string text) {
share|improve this answer
I didnt know what IMO meant, I thought it was a C# thing. I googled "C# IMO" and landed here and got the answer...lol quantnet.com/threads/c-c-vba-or-java.11433 –  electricalbah Mar 6 at 7:04

You can pass multiple arguments like this.

                    arguments.Add(argument 1);
                    arguments.Add(argument 1);
                    arguments.Add(argument n);


private void worker_DoWork(object sender, DoWorkEventArgs e) {

  List<object> genericlist = e.Argument as List<object>;
  extract your multiple arguments from this list and cast them and use them. 

share|improve this answer
how to extract multiple arguments from above code please help.. –  missReclusive Feb 25 at 9:11
@missReclusive cast the "genericlist" items,i.e,Let say "argument 1" is of type int then int argument1=(int)genericlist[0] –  Zain Ali May 6 at 14:36

You can use the DoWorkEventArgs.Argument property.

A full example (even using an int argument) can be found on Microsoft's site:

share|improve this answer

Check out the DoWorkEventArgs.Argument Property:


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    // Do not access the form's BackgroundWorker reference directly.
    // Instead, use the reference provided by the sender parameter.
    BackgroundWorker bw = sender as BackgroundWorker;

    // Extract the argument.
    int arg = (int)e.Argument;

    // Start the time-consuming operation.
    e.Result = TimeConsumingOperation(bw, arg);

    // If the operation was canceled by the user, 
    // set the DoWorkEventArgs.Cancel property to true.
    if (bw.CancellationPending)
        e.Cancel = true;
share|improve this answer

You need RunWorkerAsync(object) method and DoWorkEventArgs.Argument property.


private void worker_DoWork(object sender, DoWorkEventArgs e) {
    int argument = (int)e.Argument; //5
share|improve this answer

Your Answer


