# Passing variable to BackgroundWorker and use the same value later

I create multiple BackgroundWorker within a for-loop and each of them needs to know a special value. To simplify, it is just i in this example. When the BackgroundWorker is finished, I need to read that i again. I thought of subclassing BackgroundWorker and creating a class MyBW for that purpose which is able to store the i as value.

My example below works, but I am interested, if this is the best way to do this?

Edit: I have to add, that the variable I need to pass is a simple String, not a large object.

Fully working minimal example

using System;
using System.Text;

{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
MyBW bw = new MyBW();
bw.value = i;
bw.WorkerReportsProgress = false;
bw.DoWork += delegate(object sender, System.ComponentModel.DoWorkEventArgs eargs)
{
};
bw.RunWorkerCompleted += delegate(object sender, System.ComponentModel.RunWorkerCompletedEventArgs eargs)
{
};

bw.RunWorkerAsync();
}

// Only as a run loop that I can see the output.
while (true)
{
Console.WriteLine("Sleeping...");
}
}
}

class MyBW : System.ComponentModel.BackgroundWorker
{
/// <summary>
/// Here I store the value.
/// </summary>
public int value;
}
}

• Oh, this doesn't look good. Unfortunatelly I cannot rewrite it withasync/await but let's wait for someone who can ;-D – t3chb0t Jul 27 '16 at 18:12
• Do you have to use BackgroundWorker? Unless you're targeting an old .NET version, you really should be using newer constructs. – 404 Jul 27 '16 at 19:40
• Hello! Please don't make changes to the original post once it has been reviewed, as that invalidates the current answers. Please see our meta side on performing iterative reviews for more information! – syb0rg Jul 27 '16 at 20:01
• Have a look at Task, BackgroundWorker are really oldschool. blog.stephencleary.com/2012/02/… – hdev Jul 28 '16 at 13:52

BackgroundWorker already has a mechanism for passing and retrieving arguments. The DoWorkEventArgs has a property for a passed in method argument and a property for the DoWork result. The RunWorkerCompletedEventArgs has a property for retrieving the result. No sub-classing is necessary, although you may need to make a custom container if you need to pass/return more than one object.

for (int i = 0; i < 10; i++)
{
BackgroundWorker bw = new BackgroundWorker ();
bw.WorkerReportsProgress = false;
bw.DoWork += delegate(object sender, System.ComponentModel.DoWorkEventArgs eargs)
{
int i = (int)eargs.Arugment;  //get argument
eargs.Result = i;            //set result
};
bw.RunWorkerCompleted += delegate(object sender, System.ComponentModel.RunWorkerCompletedEventArgs eargs)
{
int i = (int)eargs.Arugment;  //get argument
};

bw.RunWorkerAsync(i);
}

• there is a bug; you defined an i inside the loop that already defines it – t3chb0t Jul 27 '16 at 18:49
• but aways it's a questionable design because a BackgroundWorker needs to be disposed and it's hard manage it with the OP's loop – t3chb0t Jul 27 '16 at 18:51
• If you are referring to the i inside the delegates, I was thinking that it would mask the outer for loop i since it is in the inner scope of a delegate. If not though, it is trivial to rename. As for the bgworkers in a loop, I've tried it before and yes, the results were mixed. – Zack Jul 27 '16 at 19:26

The solution I came up with, thanks to the many hints in the comments and answer is as following:

using System;
using System.ComponentModel;
using System.Text;

{
class Program
{
static void Main(string[] args)
{
// Run the async method 10 times.
for (int i = 0; i < 10; i++)
{
RunSomethingAsync(i);
}

// Only as a run loop so that the output is visible.
while (true)
{
Console.WriteLine("Sleeping...");
}
}

static async void RunSomethingAsync(int i)
{
int z = 99;