2011-10-25 51 views
1

所有的,我已經成功地使用了ADO.NET來使用類似於下面例子的異步SQL查詢。在所示的例子中,ExecNonQuery方法正在從UI線程中調用。這很好,但我想知道如果我要從非UI線程調用ExecNonQuery,我將如何處理回調?如何處理來自非UI線程的異步SQL查詢

注意。顯然,在這種情況下,我會修改ExecNonQuery,以便諸如this.toolStripStatusLabel1.Text這樣的事情得到相應處理或刪除。

public bool ExecNonQuery(string strCmd, string strUserMsg = "") 
{ 
    try 
    { 
     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = conn; 
     cmd.CommandText = strCmd; 
     cmd.CommandTimeout = 0; 
     bIsExecuting = true; 
     AsyncCallback callback = new AsyncCallback(HandleCallback); 
     cmd.BeginExecuteNonQuery(callback, cmd); 
     return true; 
    } 
    catch (Exception Ex) 
    { 
     bIsExecuting = false; 
     this.toolStripStatusLabel1.Text = String.Format("Ready (last error: {0})", Ex.Message); 
     if (conn != null) 
      conn.Close(); 
    } 
    return false; 
} 

private delegate void DisplayInfoDelegate(string Text); 

private void HandleCallback(IAsyncResult result) 
{ 
    try 
    { 
     // Retrieve the original command object, passed 
     // to this procedure in the AsyncState property 
     // of the IAsyncResult parameter. 

     SqlCommand command = (SqlCommand)result.AsyncState; 
     int rowCount = command.EndExecuteNonQuery(result); 
     string rowText = " rows affected."; 
     if (rowCount == 1) 
      rowText = " row affected."; 
     rowText = rowCount + rowText; 

     // Call the procedure from the form's thread. 
     DisplayInfoDelegate del = new DisplayInfoDelegate(DisplayResults); 
     this.Invoke(del, rowText); 
    } 
    catch (Exception ex) 
    { 
     // Because you are now running code in a separate thread, 
     // if you do not handle the exception here, none of your other 
     // code catches the exception. 

     // You can create the delegate instance as you 
     // invoke it, like this: 
     this.Invoke(new DisplayInfoDelegate(DisplayResults), 
      String.Format("Ready(last error: {0}", ex.Message)); 
    } 
    finally 
    { 
     bIsExecuting = false; 
     if (conn != null) 
      conn.Close(); 
    } 
} 

private void DisplayResults(string Text) 
{ 
    this.toolStripStatusLabel1.Text = Text; 
    this.toolStripProgressBar1.Style = ProgressBarStyle.Blocks; 
    this.toolStripProgressBar1.Value = 100; 
} 

感謝您的時間。

+0

我想到的第一件事就是使用併發異常。您可以在數據庫中創建一個時間戳類型字段,用於指示記錄上次更新的時間。如果您的時間戳與數據庫中的時間戳衝突,則會引發併發錯誤。 –

+0

什麼是'SynchronizationContext'或什麼UI框架你使用(ASP,WinForms,WPF等) – SliverNinja

+0

道歉。這是用WinForms完成的。 – MoonKnight

回答

1

它對你的回調沒有任何影響,哪個線程運行ExecNonQuery - HandleCallback仍然會在線程池線程上運行。

您已經發現需要做出的更改:如果不在UI線程上運行,請不要直接在ExecNonQuery中訪問UI控件。

尼克