yellowwee 2011年04月14日
如何捕获事件中的异常???
SendCMD中throw的能捕获,OnSend中不行。怎么回事?


private string SendCMD(string message)
{
throw new Exception("eeee");
sendBuffer = Encoding.Default.GetBytes(message);

//Prepare arguments for send/receive operation.
SocketAsyncEventArgs completeArgs = new SocketAsyncEventArgs();
completeArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
completeArgs.UserToken = clientSocket;
completeArgs.RemoteEndPoint = hostEndPoint;
completeArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);

//Start sending asyncronally.
clientSocket.SendAsync(completeArgs);

//Wait for the send/receive completed.
AutoResetEvent.WaitAll(autoSendReceiveEvents);

//Return data from SocketAsyncEventArgs buffer.
string sReturn = Encoding.Default.GetString(completeArgs.Buffer, completeArgs.Offset, completeArgs.BytesTransferred);

return sReturn;
}

//OnSend方法使用
private byte[] receiveBuffer = new byte[4096];

private void OnSend(object sender, SocketAsyncEventArgs e)
{
throw new Exception();

autoSendReceiveEvents[RecevieOperation].Set();

if (e.SocketError == SocketError.Success)
{
if (e.LastOperation == SocketAsyncOperation.Send)
{
//Prepare receiving.
Socket s = e.UserToken as Socket;
if (s != null)
{
//throw new OutOfMemoryException();
e.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
e.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);

s.ReceiveAsync(e);
}
}
}
else
{
ProcessError(e);
}
}
...全文
75 点赞 收藏 5
写回复
5 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告