Questions:
What is the best way to check if two BackgroundWorkers returned true as there values
or if neither returned true, or only one did.
Additional Info:
I have two BackgroundWorker currently checking to see if two SQL connections are
valid and returning a value depending whether a connection was made successfully.
The code is as follows:
private void btnTestSConnection_Click(object sender, EventArgs e)
{
BackgroundWorker work1 = new BackgroundWorker { WorkerSupportsCancellation = true };
BackgroundWorker work2 = new BackgroundWorker { WorkerSupportsCancellation = true };
work1.RunWorkerCompleted += (item, a) =>
{
//need to figure out this portion
};
work2.RunWorkerCompleted += (item, a) =>
{
//need to figure out this portion
};
work1.DoWork += doWork;
work2.DoWork += doWork;
SourceString.InitialCatalog = txtSSourceDatabase.Text;
work1.RunWorkerAsync(SourceString.ConnectionString);
SourceString.InitialCatalog = txtSSystemDatabase.Text;
work2.RunWorkerAsync(SourceString.ConnectionString);
}
DoWorkEventHandler doWork = (sender, e) =>
{
SqlConnection Connection;
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; (i <= 10); i++)
{
try
{
using (Connection = new SqlConnection((string)e.Argument))
{
Connection.Open();
}
e.Result = true;
}
catch (SqlException c)
{
e.Result = false;
}
}
};
You may returned a KeyValuePair where the first bool represent the which worker were used (true for work1, false for work2) and the second bool is the returned value of the DoWork method like this:
Now on your RunWorkerCompleted, you can cast the Result to a KeyValuePair and get if weither the work1 or work2 returned a which value.