Killing HttpWebRequest object using Thread.Abort

后端 未结 3 1757
-上瘾入骨i
-上瘾入骨i 2020-12-19 09:20

All, I am trying to cancel two concurrent HttpWebRequests using a method similar to the code below (shown in pseudo-ish C#).

The Main method creates two threads whic

3条回答
  •  Happy的楠姐
    2020-12-19 10:15

    Use BeginGetResponse to initiate the call and then use the Abort method on the class to cancel it.

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest_methods.aspx

    I believe Abort will not work with the synchronous GetResponse:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort.aspx

    If you have to stick with the synchronous version, to kill the situation, all you can do is abort the thread. To give up waiting, you can specify a timeout:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout.aspx

    If you need to kill the process, I would argue launching it inside a new AppDomain and dropping the AppDomain when you want to kill the request; instead of aborting a thread inside your main process.

提交回复
热议问题