简体   繁体   English

.NET取消流BeginRead

[英].NET cancel stream BeginRead

I have not been able to find a way to cancel/terminate asynchronous read operation after successful HttpWebRequest. 在成功的HttpWebRequest之后,我无法找到取消/终止异步读取操作的方法。 There is no way to set timeout, ThreadPool.RegisterWaitForSingleObject is not working too. 没有办法设置超时,ThreadPool.RegisterWaitForSingleObject也不起作用。 And closing the underlying socket is not an option too because neither HttpWebRequest/Reponse provide access to it. 并且关闭底层套接字也不是一个选项,因为HttpWebRequest / Reponse都不提供对它的访问。

Edit: 编辑:

Sadly this approach that Sunny suggestet works only for HttpWebRequest.BeginGetResponse. 可悲的是,Sunny的建议只适用于HttpWebRequest.BeginGetResponse。 For the stream that you get after GetResponseStream() for some reason RegisterWaitForSingleObject is not working - the callback is never called. 对于GetResponseStream()之后由于某种原因而得到的流RegisterWaitForSingleObject不起作用 - 永远不会调用回调。

The situation is the following: I got an application that uses HttpGetRequest. 情况如下:我得到了一个使用HttpGetRequest的应用程序。 It is build by using the default MSDN example for async httpwebrequest. 它是使用异步httpwebrequest的默认MSDN示例构建的。 Getting response is working like a charm. 获得回应就像魅力一样。 But on rare occasions the server that my httpwebrequest is connecting to forgets to close the socket. 但在极少数情况下,httpwebrequest连接的服务器会忘记关闭套接字。 So I am hung on a infinite read from BeginRead. 所以我挂在BeginRead的无限读取上。

In some rare occasions the other server forget 在一些罕见的情况下,其他服务器忘了

Why not RegisterWaitForSingleObject? 为什么不注册RegisterWaitForSingleObject? You can use it for time out, and in the handler you can call request.Abort(). 您可以使用它来超时,在处理程序中可以调用request.Abort()。

Here's an example . 这是一个例子 Btw, I have used simial approach before I found this article in the past, and it worked like a charm. 顺便说一下,在我过去发现这篇文章之前,我使用过simial方法,它就像一个魅力。

NOTE: The real end of the operation will happen on the next Stream.Read (of only the request/response was async) or Stream.EndRead (if the reading is async as well). 注意:操作的真正结束将发生在下一个Stream.Read(仅请求/响应是异步)或Stream.EndRead(如果读取也是异步)。 You still need to capture the exception thrown in the EndXXXX handler. 您仍然需要捕获EndXXXX处理程序中抛出的异常。

EDIT: If the RegisterWaitForSingleObject never get called, then you have another issue, which is most probably a ThreadPool having not enough free threads. 编辑:如果RegisterWaitForSingleObject永远不会被调用,那么你有另一个问题,很可能是ThreadPool没有足够的空闲线程。 The callback for RegisterWaitForSingle object is called on a ThreadPool thread, and if there is no a free thread in the pool, it will never get called. 在ThreadPool线程上调用RegisterWaitForSingle对象的回调,如果池中没有空闲线程,它将永远不会被调用。 You have some solutions: 你有一些解决方案:

  1. Make your pages asynchronous. 使您的页面异步。 Why ? 为什么 How . 怎么样
  2. Change the max threads in the pool . 更改池中最大线程数

In any case, increasing the pool size will not help you too much. 无论如何,增加池大小对你来说不会太有帮助。 You have to inspect your code for resource (stream) leaks, and make sure that you not only call request.Abort(), but also close the response. 您必须检查代码是否存在资源(流)泄漏,并确保您不仅调用request.Abort(),还要关闭响应。 Make sure than in your callbacks you use the proper EndXXX method, etc. 确保在回调中使用正确的EndXXX方法等。

I nazdrave :) 我nazdrave :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM