为什么WebClient.DownloadStringTaskAsync()块? - 新的异步API /语法/ CTP [英] Why does WebClient.DownloadStringTaskAsync() block ? - new async API/syntax/CTP

查看:211
本文介绍了为什么WebClient.DownloadStringTaskAsync()块? - 新的异步API /语法/ CTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,有一个暂停下面的程序启动后。我相信, Web客户端()。DownloadStringTaskAsync()的原因。

 类节目
{
    静态无效的主要(字串[] args)
    {
        AsyncReturnTask();

        的for(int i = 0; I< 15;我++)
        {
            Console.WriteLine(ⅰ);
            Thread.sleep代码(100);
        }
    }

    公共静态异步无效AsyncReturnTask()
    {
        VAR的结果=等待DownloadAndReturnTaskStringAsync();
        Console.WriteLine(结果);
    }

    私有静态异步任务<字符串> DownloadAndReturnTaskStringAsync()
    {
        返回等待新的Web客户端()DownloadStringTaskAsync(新的URI(http://www.weather.gov))。
    }
}
 

据我了解我的计划应该开始计数,从0到15,立即。难道我做错了什么?

我有同样的问题与原来的Netflix下载的样品(其中你与 CTP ) - $后p $ pssing搜索按钮的UI先冻结 - 而经过一段时间后它响应而loadning下一个电影。而且我相信它没有安德斯·海尔斯伯格的presentation在PDC 2010冷冻。

一件事。而不是当

 返回等待新的Web客户端()DownloadStringTaskAsync(新的URI(http://www.weather.gov))。
 

我用我自己的方法:

 返回等待ReturnOrdinaryTask();
 

这就是:

 公共静态任务<字符串> ReturnOrdinaryTask()
{
    VAR T = Task.Factory.StartNew(()=>
    {
        的for(int i = 0;我小于10;我++)
        {
            Console.WriteLine(-------------+ i.ToString());
            Thread.sleep代码(100);
        }
        返回一些文本;
    });
    返回吨;
}
 

它的工作原理,因为它应该。我的意思是它不会加载任何东西,但它会立即启动,并不会阻止主线程,同时做其工作。

修改

OK,我认为现在的问题是:在 WebClient.DownloadStringTaskAsync 函数搞砸了。这样就没有了最初的封锁期,像这样的:

 静态无效的主要(字串[] args)
    {
        Web客户端CLI =新的Web客户端();
        Task.Factory.StartNew(()=>
            {
                cli.DownloadStringCompleted + =(发件人,E)=> Console.WriteLine(e.Result);
                cli.DownloadStringAsync(新的URI(http://www.weather.gov));
            });

        的for(int i = 0; I< 100;我++)
        {
            Console.WriteLine(ⅰ);
            Thread.sleep代码(100);
        }
    }
 

解决方案

当你的程序不会阻止了一段时间,但它确实恢复执行在for循环之前,结果从远程服务器返回。

记住,新的异步API仍然是单线程的。因此, Web客户端()。DownloadStringTaskAsync()仍需要在你的线程运行,直到该请求已经prepared和发送到服务器,才能够等待和产量的执行回你的程序流程在main()。

我认为你所看到的结果是由于它需要一些时间来创建并发送请求从您的计算机的事实。首先当完成后, DownloadStringTaskAsync 的实施可以等待网络IO和远程服务器来完成,并且可以返回运行到你。

在另一方面,你的 RunOrdinaryTask 方法只是初始化任务,并赋予它一个工作量,并告诉它启动。然后,它会立即返回。这就是为什么你在使用没有看到一个延时 RunOrdinaryTask

下面是一些关于这个问题的链接:埃里克利珀的博客(语言的设计师之一),以及乔恩斯基特最初的博客文章一下吧。埃里克有一系列的5个员额约延续传递风格,这到底是什么异步等待实际上是。如果您想详细了解了新的功能,你可能需要阅读Eric的关于CPS和异步的职位。不管怎么说,这两个环节上面并讲解一个非常重要的事实一份好工作:

  • 异步!=并行

在换句话说,异步等待不旋转起来新主题你。他们只是让你恢复执行正常的流动,当你正在做一个阻塞操作 - 时间,你的CPU就只是坐着什么也不做在同步程序,等待一些外部操作完成

修改

只是要清楚地了解正在发生的事情: DownloadStringTaskAsync 设置延续,然后调用 WebClient.DownloadStringAsync ,在同一个线程,而的收益率执行返回到您的code。因此,您所看到前循环开始计数阻断时间,是需要 DownloadStringAsync 来完成的时间。您的程序与异步和等待是非常接近的是下面的程序,它表现出相同的行为,你的程序等价的:一个初始块,然后开始计数,并在中间某个地方,异步操作完成,并打印从内容所请求的网址:

 静态无效的主要(字串[] args)
    {
        Web客户端CLI =新的Web客户端();
        cli.DownloadStringCompleted + =(发件人,E)=> Console.WriteLine(e.Result);
        cli.DownloadStringAsync(新的URI(http://www.weather.gov)); //阻塞,直到请求已经prepared

        的for(int i = 0; I< 15;我++)
        {
            Console.WriteLine(ⅰ);
            Thread.sleep代码(100);
        }
    }
 

注:我绝不是在这方面的专家,所以我可能是错的一些问题。随时纠正我对主题的理解,如果你认为这是错误的 - 我只是看着PDC presentation,并使用了与CTP昨晚

For some reason there is a pause after the program below starts. I believe that WebClient().DownloadStringTaskAsync() is the cause.

class Program
{
    static void Main(string[] args)
    {
        AsyncReturnTask();

        for (int i = 0; i < 15; i++)
        {
            Console.WriteLine(i);
            Thread.Sleep(100);
        }
    }

    public static async void AsyncReturnTask()
    {
        var result = await DownloadAndReturnTaskStringAsync();
        Console.WriteLine(result);
    }

    private static async Task<string> DownloadAndReturnTaskStringAsync()
    {
        return await new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov"));
    }
}

As far as I understand my program should start counting from 0 to 15 immediately. Am I doing something wrong?

I had the same problem with the original Netflix download sample (which you get with CTP) - after pressing the search button the UI first freezes - and after some time it is responsive while loadning the next movies. And I believe it didn't freeze in Anders Hejlsberg's presentation at PDC 2010.

One more thing. When instead of

return await new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov"));

I use my own method:

return await ReturnOrdinaryTask();

Which is:

public static Task<string> ReturnOrdinaryTask()
{
    var t = Task.Factory.StartNew(() =>
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("------------- " + i.ToString());
            Thread.Sleep(100);
        }
        return "some text";
    });
    return t;
}

It works as it should. I mean it doesn't load anything, but it starts immediately and doesn't block the main thread, while doing its work.

Edit

OK, what I believe right now is: the WebClient.DownloadStringTaskAsync function is screwed up. It should work without the initial blocking period, like this:

    static void Main(string[] args)
    {
        WebClient cli = new WebClient();
        Task.Factory.StartNew(() =>
            {
                cli.DownloadStringCompleted += (sender, e) => Console.WriteLine(e.Result);
                cli.DownloadStringAsync(new Uri("http://www.weather.gov"));
            });

        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine(i);
            Thread.Sleep(100);
        }
    }

解决方案

While your program does block for a while, it does resume execution in the for loop, before the result is returned from the remote server.

Remember that the new async API is still single-threaded. So WebClient().DownloadStringTaskAsync() still needs to run on your thread until the request has been prepared and sent to the server, before it can await and yield execution back to your program flow in Main().

I think the results you are seeing are due to the fact that it takes some time to create and send the request out from your machine. First when that has finished, the implementation of DownloadStringTaskAsync can wait for network IO and the remote server to complete, and can return execution to you.

On the other hand, your RunOrdinaryTask method just initializes a task and gives it a workload, and tells it to start. Then it returns immediately. That is why you don't see a delay when using RunOrdinaryTask.

Here are some links on the subject: Eric Lippert's blog (one of the language designers), as well as Jon Skeet's initial blog post about it. Eric has a series of 5 posts about continuation-passing style, which really is what async and await is really about. If you want to understand the new feature in detail, you might want to read Eric's posts about CPS and Async. Anyways, both links above does a good job on explaining a very important fact:

  • Asynchronous != parallel

In other words, async and await does not spin up new threads for you. They just lets you resume execution of your normal flow, when you are doing a blocking operation - times where your CPU would just sit and do nothing in a synchronous program, waiting for some external operation to complete.

Edit

Just to be clear about what is happening: DownloadStringTaskAsync sets up a continuation, then calls WebClient.DownloadStringAsync, on the same thread, and then yields execution back to your code. Therefore, the blocking time you are seeing before the loop starts counting, is the time it takes DownloadStringAsync to complete. Your program with async and await is very close to be the equivalent of the following program, which exhibits the same behaviour as your program: An initial block, then counting starts, and somewhere in the middle, the async op finishes and prints the content from the requested URL:

    static void Main(string[] args)
    {
        WebClient cli = new WebClient();
        cli.DownloadStringCompleted += (sender, e) => Console.WriteLine(e.Result);
        cli.DownloadStringAsync(new Uri("http://www.weather.gov")); // Blocks until request has been prepared

        for (int i = 0; i < 15; i++)
        {
            Console.WriteLine(i);
            Thread.Sleep(100);
        }
    }

Note: I am by no means an expert on this subject, so I might be wrong on some points. Feel free to correct my understanding of the subject, if you think this is wrong - I just looked at the PDC presentation and played with the CTP last night.

这篇关于为什么WebClient.DownloadStringTaskAsync()块? - 新的异步API /语法/ CTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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