Task<WebResponse>.Wait 永远持续 [英] Task&lt;WebResponse&gt;.Wait lasts forever

查看:21
本文介绍了Task<WebResponse>.Wait 永远持续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#、.Net 4.5.我有一个包含要处理的对象的队列.处理包括通过在对象的字段之一中指定的 URL 获取数据.在操作过程中,可以将新对象添加到队列中.当我尝试使用网络异步进行工作时,我遇到了一个问题.

C#, .Net 4.5. I have a queue that contains objects which are to be processed. Processing includes obtaining data with the URL which specified in one of the fields of the object. In the course of operation the new objects can be added to the queue. When I tried to do the work with the network asynchronous, I faced with a problem.

这是一个最低代码.

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        string[] urls = { "http://www.stackoverflow.com/", 
                            "http://www.google.com/", 
                            "http://www.microsoft.com/" };
        int i = 0;
        Queue<MyClass1> queue = new Queue<MyClass1>();

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urls[i]);
        webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
        queue.Enqueue(new MyClass1(urls[i], webRequest.GetResponseAsync()));

        while (queue.Count > 0)
        {
            MyClass1 o = queue.Dequeue();
            o.RespTask.Wait();
            Debug.Print("Url: {0}, bytes: {1}", o.Url, 
                o.RespTask.Result.ContentLength);

            i++;
            if (i < urls.Length)
            {
                webRequest = (HttpWebRequest)WebRequest.Create(urls[i]);
                webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
                queue.Enqueue(new MyClass1(urls[i], webRequest.GetResponseAsync()));
            }
        } 
    }
}

public class MyClass1
{
    public MyClass1() { }
    public MyClass1(string url, Task<WebResponse> respTask)
    {
        Url = url;
        RespTask = respTask;
    }

    public string Url;
    public Task<WebResponse> RespTask;
}

该代码挂在 o.RespTask.Wait();在循环的第三次迭代中.在此调用之前 o.RespTask.Status 具有值 WaitingForActivation 并且等待将永远持续.我做错了什么?

That code hangs on o.RespTask.Wait(); on third iteration of cycle. Before this call o.RespTask.Status has value WaitingForActivation and waiting lasts forever. What am I doing wrong?

更新.我检查了 3 个盒子上的代码.在其中两个(Win7 32 位和 Win7 64 位)上,程序挂起.在第三个(Win7 64 位)上一切正常.在我看来很奇怪.

UPDATE. I checked the code on 3 boxes. On two of them (Win7 32-bit and Win7 64-bit) the program hangs. On the third (Win7 64-bit) everything is working fine. It seems to me very strange.

推荐答案

这样修改后这段代码就不再挂了:

This code has ceased to hang after such a modification:

...
Debug.Print("Url: {0}, bytes: {1}", o.Url, 
    o.RespTask.Result.ContentLength);

o.RespTask.Result.Close();

i++;
...

我的错误是没有注意调用HttpWebResponse类的Close方法是必须的.

My mistake was that I did not pay attention to the fact that the call to the Close method of the HttpWebResponse class is a must.

这篇关于Task<WebResponse>.Wait 永远持续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆