异步执行的返回值...
.net 2.0中引入了异步的数据库执行,比如说原来的ExecuteNonQuery有了对应的BeginExecuteNonQuery,EndExecuteNonQuery,如下:
自己的一点想法,不一定很好,也许还有更好的方法。
public void ExecuteAsyncNonQuery(SqlConnection conn, CommandType commandType, string commandText, params SqlParameter[] param)
{
SqlCommand cmd = new SqlCommand();
//SqlTransaction trans=this.Conn.BeginTransaction();
try
{
PrepareCommand(cmd, conn, (SqlTransaction)null, commandType, commandText, param);
AsyncCallback callback = new AsyncCallback(HandleCallback);
cmd.BeginExecuteNonQuery(callback, cmd);
}
catch (Exception ex)
{
OnError(ex.Message);
throw ex;
}
//finally
//{
// cmd = null;
//}
}
private void HandleCallback(IAsyncResult ar)
{
SqlCommand cmd;
try
{
cmd= (SqlCommand)ar.AsyncState;
int rowCount = cmd.EndExecuteNonQuery(ar);
OnRowAffect(rowCount);
}
catch
{
}
finally
{
cmd = null;
}
}
{
SqlCommand cmd = new SqlCommand();
//SqlTransaction trans=this.Conn.BeginTransaction();
try
{
PrepareCommand(cmd, conn, (SqlTransaction)null, commandType, commandText, param);
AsyncCallback callback = new AsyncCallback(HandleCallback);
cmd.BeginExecuteNonQuery(callback, cmd);
}
catch (Exception ex)
{
OnError(ex.Message);
throw ex;
}
//finally
//{
// cmd = null;
//}
}
private void HandleCallback(IAsyncResult ar)
{
SqlCommand cmd;
try
{
cmd= (SqlCommand)ar.AsyncState;
int rowCount = cmd.EndExecuteNonQuery(ar);
OnRowAffect(rowCount);
}
catch
{
}
finally
{
cmd = null;
}
}
比如说原来我想通过ExecuteNonQuery得到返回值,检查执行多少行,
(备注:今天看了篇文章,如果ExecuteNonQuery执行中有触发器执行,那么返回的affected rows为executenonquery和触发器执行的总和,具体没有验证过)
那么现在直接执行ExecuteAsyncNonQuery是拿不到返回值的,怎么办呢,我这里想到的一般是CallBack的时候得到执行EndExecuteNonQuery后触发事件,将返回值以事件的方式抛出。如上文中的OnRowAffect,调用端订阅这个事件。
如这里的Socket调用,也是同样方法:
//TODO:Socket关闭nagle算法
//微软nagle算法。为了有效利用带宽,nagle算法会使得Socket发送短数据包尽量合并发送,就产生一个延迟。
socketSB.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
AsyncCallback async=new AsyncCallback(CallBack);
socketSB.BeginSend(bts, 0, bts.Length, SocketFlags.None, out errorcode, async, socketSB);
return;
private void CallBack(IAsyncResult ar)
{
if (ar.AsyncWaitHandle.WaitOne())
{
// Retrieve the socket from the state object.
Socket socketSB = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = socketSB.EndSend(ar);
if (bytesSent == 0)
{
OnSocketSendError("Send failed");
}
else
{
OnSocketSendError("Success");
}
}
}
//微软nagle算法。为了有效利用带宽,nagle算法会使得Socket发送短数据包尽量合并发送,就产生一个延迟。
socketSB.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
AsyncCallback async=new AsyncCallback(CallBack);
socketSB.BeginSend(bts, 0, bts.Length, SocketFlags.None, out errorcode, async, socketSB);
return;
private void CallBack(IAsyncResult ar)
{
if (ar.AsyncWaitHandle.WaitOne())
{
// Retrieve the socket from the state object.
Socket socketSB = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = socketSB.EndSend(ar);
if (bytesSent == 0)
{
OnSocketSendError("Send failed");
}
else
{
OnSocketSendError("Success");
}
}
}
自己的一点想法,不一定很好,也许还有更好的方法。