passing on KeyValuePair list from backgroundWorker1 to backgroundWorker2

I have a little problem, I'm using

var filelist = new List<KeyValuePair<string, string>>();
 filelist.Add(new KeyValuePair<string, string>(a, b));
 filelist.Add(new KeyValuePair<string, string>(c,d));

in backgroundWorker1, to do what it needs to do, and when backgroundWorker1 is done, the list should be passed on to backgroundWorker2 somehow

I have no clue where to even begin doing that so any help is much appreciated.

Answers


Have the first's RunWorkerCompleted start the second with an argument. Here's an example just for an idea of how to send an argument from the first to the second.

public Form1()
{
    InitializeComponent();
    backgroundWorker1.DoWork += backgroundWorker1_DoWork;
    backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

    backgroundWorker2.DoWork += backgroundWorker2_DoWork;
    backgroundWorker2.RunWorkerCompleted += backgroundWorker2_RunWorkerCompleted;

    backgroundWorker1.RunWorkerAsync();
}


void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    e.Result = "a";
}

void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    string s = (string)e.Result;
    backgroundWorker2.RunWorkerAsync(s);
}

void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
    e.Result = (string)e.Argument;
}

void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Text = (string)e.Result;
}

Make sure the list is visible to both workers and also an event variable (it can be an AutoresetEvent for example). Then when the work on the list is finished in backgroundworker1, signal the second worker to read it:

// prepare the list
event.Set();

In backgroundworker2 you should wait at some point on the event:

event.WaitOne();
// use the list

You can simply start the second BackgroundWorker in the delegate that you're using to handle the first BackgroundWorker's RunWorkerCompleted event.

Just set the args in the RunWorkerAsync call of BW#2 to the return value of BW#1.

It doesn't sound like BW#2 has anything to do before BW#1 has finished, so there's no point implementing shared state or raising events or implementing an observer-type pattern.


Need Your Help

Unable to retrieve a sub-NodeList from a NodeList in Javascript

javascript android xml cordova

So here's the deal: I'm trying to retrieve a NodeList object from an already existing NodeList. Here's a simplified XML example:

Login method Customization using GINA

c# .net windows-services login ipc

DUPLICATE:http://stackoverflow.com/questions/523912/login-method-customization-using-gina

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.