2012-09-27 90 views
2

問題: 什麼是檢查兩個BackgroundWorkers因爲有值返回true的最佳途徑 或者如果沒有返回true,或者只有1。確定如果兩個BackgroundWorker的字符串返回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

您可能返回一個KeyValuePair其中第一布爾表示使用了該工人(真爲WORK1,用於WORK2假),第二個布爾是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上,您可以將結果轉換爲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

您可以使用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; 
        } 
       } 
     }; 
+0

此代碼將會死鎖。不要在UI線程上調用WaitOne()。 –

+0

這是非常真實的。所以在你想做這件事之前,先轉到另一個線程。 –

1

您可以使用關鍵字volatile一個布爾變量。並在線程內改變它的值。並在完成後或工作過程中的任何時間檢查它

相關問題