确定两个BackGroundWorker字符串是否返回true

时间:2012-09-27 14:44:11

标签: c# multithreading

问题:      检查两个BackgroundWorkers是否返回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;
                    }
                }
        };

3 个答案:

答案 0 :(得分: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
};

答案 1 :(得分:1)

每次完成时,您都可以使用wait handle来触发事件。

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;
                    }
                }
        };

答案 2 :(得分:1)

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