質問:2つのBackgroundWorkerが値としてtrueを返したかどうか、どちらもtrueを返さなかったか、または1つだけが返したかを確認するための最良の方法は何ですか。
追加情報:
現在、2つのBackgroundWorkerが2つのSQL接続が有効かどうかを確認し、接続が正常に行われたかどうかに応じて値を返しています。
コードは次のとおりです。
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;
}
}
};