I'm creating an array of BackgroundWorker that shares one event handler like this:
BackgroundWorker[] workers = new BackgroundWorker[files.length];
for(int i = o; i<files.length; i++)
{
workers[i] = new BackgroundWorker();
workers[i].DoWork += new DoWorkEventHandler(worker_DoWork);
workers[i].RunWorkerCompleted += newRunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
workers[i].RunWorkerAsync(files[i]);
}
All workers are sharing the same event handler which does same thing only with different argument and result like this:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeSomething(e.Argument.ToString());
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
resultArray.Add(e.Result);
}
private int ComputeSomething(string file)
{
...
return number;
}
obviously in the code, I'm trying to make a list of BackgroundWorker that runs asyncronously but when I checked the results, some of them are incorrect. I'm guessing that the value of "e.result" were replaced by other workers since they share the same event handler if that is the case then i would like to create Individual event handlers for each BackgroundWorker so that the value of e.result won't get replaced. How will i do that?