本文介绍了取消一个Socket.xxxxAsync电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主叫Socket.Shutdown,Socket.Close和Socket.Disconnect后,看来Socket.ReceiveAsync不会中止。尝试重用在ReceiveAsync调用(文档的最佳实践建议)中使用的SocketAsycEventArgs对象产生错误:

After calling Socket.Shutdown, Socket.Close and Socket.Disconnect, it appears that Socket.ReceiveAsync does not abort. Attempts to reuse the SocketAsycEventArgs object used in the ReceiveAsync call (suggested in the documentation as best practice) result in the error:

这是异步套接字操作已在使用这种的SocketAsyncEventArgs实例执行

我有什么做的就是ReceiveAsync释放其握在此的SocketAsyncEventArgs实例?

What do I have to do to get ReceiveAsync to release its grip on this SocketAsyncEventArgs instance?

编辑:我已经通过标记挂起的接收并没有做任何的清理解决此工作,直到接收进来(即完成分派)。感觉并不太好,但。不能只是被中止,如WebRequest的API?

I have worked around this by flagging a pending receive and not doing any cleanup until the receive comes in (i.e Completed is dispatched). Doesn't feel too good though. Can't it just be aborted, like the WebRequest API?

推荐答案

似乎没有成为一个办法中止异步接收。

There does not appear to be a way to abort the asynchronous receive.

在关闭套接字,这迫使接收完成,并且让SocketAsyncEventArgs参数回调方法的SocketError属性将有 SocketError.OperationAborted 的值。当遇到这种情况,可以在让SocketAsyncEventArgs对象返回到可重复使用的游泳池。

When the socket is closed, this forces the receive to complete, and the SocketError property of the SocketAsyncEventArgs parameter to your callback method will have a value of SocketError.OperationAborted. When this condition is encountered, you can return the SocketAsyncEventArgs object to the reusable pool.

这是体现在这里的例子一>。具体来说,看ProcessReceive()方法,该方法调用CloseClientSocket()方法时, e.BytesTransferred == 0 e.SocketError!= SocketError .Success 。所述CloseClientSocket()方法是,其中的SocketAsyncEventArgs对象被返回到池中。

This is demonstrated in the example shown here. Specifically, look at the ProcessReceive() method, which calls the CloseClientSocket() method when e.BytesTransferred == 0 or e.SocketError != SocketError.Success. The CloseClientSocket() method is where the SocketAsyncEventArgs object is returned to the pool.

这篇关于取消一个Socket.xxxxAsync电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:26