2011-10-25 29 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控件。

尼克