任务<&WebResponse的GT; .Wait永恒 [英] Task<WebResponse>.Wait lasts forever

查看:234
本文介绍了任务<&WebResponse的GT; .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.

下面是一个最小的代码。

Here is a minimum code.

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.

这篇关于任务&LT;&WebResponse的GT; .Wait永恒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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