Cancel .NET BeginRead Stream

I could not find a way to cancel / terminate the asynchronous read operation after a successful HttpWebRequest. Unable to set timeout, ThreadPool.RegisterWaitForSingleObject does not work either. And closing the base socket is not an option, because neither HttpWebRequest / Reponse provides access to it.

Edit:

Unfortunately, this approach that Sunny advises only works for HttpWebRequest.BeginGetResponse. For the stream that you get after GetResponseStream () for some reason RegisterWaitForSingleObject does not work - the callback is never called.

The situation is this: I got an application that uses HttpGetRequest. It is created using the standard MSDN example for async httpwebrequest. Getting an answer works like a charm. But in rare cases, the server my httpwebrequest is connecting to forgets to close the socket. So I hung on an endless read from BeginRead.

In some cases, another server forgets

+3
source share
1 answer

Why not RegisterWaitForSingleObject? You can use it for a timeout, and in the handler you can call request.Abort ().

Here is an example . By the way, I used a similar approach before I found this article in the past, and it worked like a charm.

. . ( / ) Stream.EndRead( ). , EndXXXX.

EDIT: RegisterWaitForSingleObject , , , , ThreadPool . RegisterWaitForSingle ThreadPool, , . :

  • . ? .
  • .

. () , request.Abort(), . , EndXXX ..

I nasdrave:)

+1

Source: https://habr.com/ru/post/1699540/


All Articles