异步 CTP 中的“TaskEx.WhenAll”是什么?

发布于 2024-10-02 03:01:10 字数 1433 浏览 0 评论 0原文

我认为 TaskEx.WhenAll 会在方法内传递的所有任务完成时返回。因此,await on TaskEx.WhenAll 将返回 Return 语句的数组,这样当每个对象完成时,都会返回该数组。

但事实并非如此。当我这样做时:

    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));
        }

    }

我看到我的 MessageBox 出现多次,并且还等待 1 个元素的数组,然后是 2 个元素的数组,依此类推。

谁能告诉我 TakEx.WhenAll 的本质是什么?

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));
        }

    }

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-10-09 03:01:10

对 TaskEx.WhenAll 的调用发生在 for 循环内。你必须把它放在外面。

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!");

    }

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!");

    }
铃予 2024-10-09 03:01:10

WhenAll() 创建一个当所有子任务完成时完成的任务。因此,方法本身不会完成,但任务会完成。

它是一种创建新任务的方法,将单独的任务聚合成一个新的、更大的任务。

WhenAll() create a tasks that completes when all the sub tasks complete. So, the method itself will not complete, but the task will.

It is a method that creates a new task that aggregates the separate task into a new, larger task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文