什么是`TaskEx.WhenAll`在异步CTP? [英] What is `TaskEx.WhenAll` in the Async CTP?

查看:188
本文介绍了什么是`TaskEx.WhenAll`在异步CTP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当所有任务完成后得到该方法中传递TaskEx.WhenAll将返回。因此,等待上TaskEx.WhenAll将返回return语句,这样,当每一个对象被完成后,阵列将返回数组。

I thought TaskEx.WhenAll would return when all the tasks gets finished which is passed within the method. So await on TaskEx.WhenAll would return the array of Return statements, such that when every object gets finished, the array will be returned.

但它并非如此。当我这样做:

But it is not so. When I do :

    public async Task AsynchronousCallServerMordernParallelAsync()
    {

        List<Task<string>> lstTasks = new List<Task<string>>();

        StringBuilder builder = new StringBuilder();

        for (int i = 2; i <= 10; i++)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    this.tbStatus.Text = string.Format("Calling Server [{0}]..... ", i);
                    string currentCall = string.Format(Feed, i);
                    Task<string> task = client.DownloadStringTaskAsync(new Uri(currentCall));
                    lstTasks.Add(task);

                }
                catch (Exception ex)
                {
                    this.tbStatus.Text = string.Format("Error Occurred -- {0} for call :{1}, Trying next", ex.Message, i);
                }
            }

            string[] rss = await TaskEx.WhenAll<string>(lstTasks);
            foreach(string s in rss)
                builder.Append(s);

            MessageBox.Show(string.Format("Downloaded Successfully!!! Total Size : {0} chars.", builder.Length));
        }

    }

我看到我的消息框出现不止一次,也等待着与1元,那么2元等阵列阵列步骤。

I see my MessageBox appears more than once, and also await steps in with array of 1 element, then array of 2 element and so on.

谁能告诉我什么TakEx.WhenAll的确切性质?

Can anyone tell me what exactly the nature of TakEx.WhenAll?

推荐答案

要TaskEx.WhenAll调用内部出现循环。你必须把它放在外面。

The call to TaskEx.WhenAll occurs inside the for loop. You have to put it outside.

public static async Task AsynchronousCallServerMordernParallelAsync()
    {
        List<Task<string>> lstTasks = new List<Task<string>>();

        StringBuilder builder = new StringBuilder();

        for (int i = 2; i <= 10; i++)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    Console.WriteLine("Calling server...");
                    Task<string> task = client.DownloadStringTaskAsync(new Uri("http://www.msn.com"));
                    lstTasks.Add(task);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error occurred!");
                }
            }

        }

        string[] rss = await TaskEx.WhenAll<string>(lstTasks);
        foreach (string s in rss)
            builder.Append(s);

        Console.WriteLine("Downloaded!");

    }

这篇关于什么是`TaskEx.WhenAll`在异步CTP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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