2

问题:检查两个 BackgroundWorkers 是否返回 true 的最佳方法是什么,或者两者都没有返回 true,或者只有一个返回 true。

附加信息:

我有两个 BackgroundWorker 目前正在检查两个 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;
                    }
                }
        };
4

3 回答 3

3

您可能会返回一个 KeyValuePair,其中第一个 bool 表示使用了哪个 worker(对于 work1,true,对于 work2 是 false),第二个 bool 是 DoWork 方法的返回值,如下所示:

work1.DoWork += doWork;
work2.DoWork += doWork;

work1.RunWorkerAsync(true);
work2.RunWorkerAsync(false);

private void doWork(s, e)
{
  var kvp = new KeyValuePair<bool, bool>;
  kvp.Key = e.Argument as bool; // this indicate which of the worker returned a value
  ...
  using (Connection = new SqlConnection((string)e.Argument))
  {
    Connection.Open();
  }
  kvp.Value = true; // this is the result of your connection test
  ...
  e.Result = kvp
};

现在在您的 RunWorkerCompleted 上,您可以将 Result 转换为 KeyValuePair 并获取 work1 或 work2 是否返回了哪个值。

b.RunWorkerCompleted += (item, a) =>
{
  var kvp = a.Result as KeyValuePair<bool, bool>;
  //kvp.Key == true mean this is the work1
  //kvp.Value is the SQL connection test
};
于 2012-09-27T15:15:16.127 回答
1

您可以volatile在布尔变量上使用关键字。并在线程内更改其值。并在完成后或工作过程中的任何时间检查

于 2012-09-27T16:19:05.763 回答
1

您可以使用等待句柄在每次完成时触发事件。

private void btnTestSConnection_Click(object sender, EventArgs e)
        {
            EventWaitHandle firstComplete = new EventWaitHandle(false, EventResetMode.ManualReset);
            EventWaitHandle secondComplete = new EventWaitHandle(false, EventResetMode.ManualReset);

                bool overallResult = false;

            BackgroundWorker work1 = new BackgroundWorker { WorkerSupportsCancellation = true };
            BackgroundWorker work2 = new BackgroundWorker { WorkerSupportsCancellation = true };
            work1.RunWorkerCompleted += (item, a) =>
            {
                firstComplete.Set();
                //need to figure out this portion
                overallResult &= a.Result 
            };
            work2.RunWorkerCompleted += (item, a) =>
            {
                secondComplete.Set();
                //need to figure out this portion
                overallResult &= a.Result 
            };

            work1.DoWork += doWork;
            work2.DoWork += doWork;

            SourceString.InitialCatalog = txtSSourceDatabase.Text;
            work1.RunWorkerAsync(SourceString.ConnectionString);
            SourceString.InitialCatalog = txtSSystemDatabase.Text;
            work2.RunWorkerAsync(SourceString.ConnectionString);

            // Wait on First will not go until set
            firstComplete.WaitOne();

            // Wait on second
            secondComplete.WaitOne();

            // Both now complete
            //Do what you need to now
        }

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;
                    }
                }
        };
于 2012-09-27T15:01:25.473 回答