杀死使用Thread.Abort的HttpWebRequest对象 [英] Killing HttpWebRequest object using Thread.Abort
问题描述
所有,我试图使用类似于下面的代码的方法来取消两个并发HttpWebRequests(伪上下的C#中所示)。
Main方法创建两个它创建线程HttpWebRequests。如果用户希望,他们可以通过点击一个按钮,然后调用abort方法中止请求
专用螺纹第一;
私人螺纹第二;
私人字符串URI =HTTP://地方;
公共无效的主要()
{
第一=新主题(GetFirst);
first.Start();
秒=新主题(GetSecond);
second.Start();
//上线......像倒数类
countdown.Wait一些块();
}
公共无效中止()
{
试
{
first.Abort();
}
抓{//什么都不做}
试
{
second.Abort();
}
抓{//什么都不做}
}
私人无效GetFirst(对象状态)
{
MyHandler的H = MyHandler的新(URI);
h.RunRequest();
}
私人无效GetSecond(对象状态)
{
MyHandler的H = MyHandler的新(URI);
h.RunRequest();
}
第一个线程得到由SocketException异常中断:
A阻塞操作被WSACancelBlockingCall
通话
$中断b $ b
第二个线程挂起的GetResponse()。
我怎么能放弃这两个要求的方式,Web服务器知道该连接已被终止?和/或有没有更好的方式来做到这一点?
更新
的建议,一个很好的选择是使用BeginGetResponse。但是,我没有访问HttpWebRequest对象 - 它是在 MyHandler的
类抽象。我已经修改的问题,以显示这一点。
公共类MyHandler的
{
公共无效RunRequest(串URI)
{
HttpWebRequest的REQ = HttpWebRequest.Create(URI);
HttpWebResponse解析度= req.GetResponse();
}
}
使用 BeginGetResponse
来发起呼叫,然后使用中止
方法的类来取消它。
的http:// MSDN。 microsoft.com/en-us/library/system.net.httpwebrequest_methods.aspx
我相信中止
将不与同步的GetResponse
工作:
的 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort.aspx
如果你坚持使用同步版本,杀的情况下,所有你能做的就是放弃线程。要放弃等待,你可以指定一个超时:
的 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout.aspx
如果你需要杀死的过程中,我认为推出一个新的AppDomain内,丢弃的AppDomain当你想杀死的要求;而不是放弃你的主要过程中的一个线程。
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 which create HttpWebRequests. If the user wishes to, they may abort the requests by clicking a button which then calls the Abort method.
private Thread first;
private Thread second;
private string uri = "http://somewhere";
public void Main()
{
first = new Thread(GetFirst);
first.Start();
second = new Thread(GetSecond);
second.Start();
// Some block on threads... like the Countdown class
countdown.Wait();
}
public void Abort()
{
try
{
first.Abort();
}
catch { // do nothing }
try
{
second.Abort();
}
catch { // do nothing }
}
private void GetFirst(object state)
{
MyHandler h = new MyHandler(uri);
h.RunRequest();
}
private void GetSecond(object state)
{
MyHandler h = new MyHandler(uri);
h.RunRequest();
}
The first thread gets interrupted by a SocketException:
A blocking operation was interrupted by a call to WSACancelBlockingCall
The second thread hangs on GetResponse().
How can I abort both of these requests in a way that the web server knows that the connection has been aborted?, and/or, Is there a better way to do this?
UPDATE
As suggested, a good alternative would be to use BeginGetResponse. However, I don't have access to the HttpWebRequest object - it is abstracted in the MyHandler
class. I have modified the question to show this.
public class MyHandler
{
public void RunRequest(string uri)
{
HttpWebRequest req = HttpWebRequest.Create(uri);
HttpWebResponse res = req.GetResponse();
}
}
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.
这篇关于杀死使用Thread.Abort的HttpWebRequest对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!