使用 Task.WaitAll() 处理等待的任务? [英] Use Task.WaitAll() to handle awaited tasks?

查看:20
本文介绍了使用 Task.WaitAll() 处理等待的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我想做的是以非阻塞模式延迟任务,然后等待所有任务完成.我尝试添加 Task.Delay 返回的任务对象,然后使用 Task.WaitAll 但这似乎无济于事.我该如何解决这个问题?

class 程序{公共静态异步无效 Foo(int num){Console.WriteLine("Thread {0} - Start {1}", Thread.CurrentThread.ManagedThreadId, num);var newTask = Task.Delay(1000);TaskList.Add(newTask);等待新任务;Console.WriteLine("Thread {0} - End {1}", Thread.CurrentThread.ManagedThreadId, num);}公共静态列表<任务>TaskList = new List();public static void Main(string[] args){for (int i = 0; i <3; i++){int idx = i;TaskList.Add(Task.Factory.StartNew(() => Foo(idx)));}Task.WaitAll(TaskList.ToArray());}}

解决方案

这是您想要实现的目标吗?

使用系统;使用 System.Collections.Generic;使用 System.Threading;使用 System.Threading.Tasks;命名空间控制台应用程序{课程计划{公共静态异步任务 Foo(int num){Console.WriteLine("Thread {0} - Start {1}", Thread.CurrentThread.ManagedThreadId, num);等待 Task.Delay(1000);Console.WriteLine("Thread {0} - End {1}", Thread.CurrentThread.ManagedThreadId, num);}公共静态列表<任务>TaskList = new List();public static void Main(string[] args){for (int i = 0; i <3; i++){int idx = i;TaskList.Add(Foo(idx));}Task.WaitAll(TaskList.ToArray());Console.WriteLine("按回车退出...");Console.ReadLine();}}}

输出:

<前>线程 10 - 开始 0主题 10 - 开始 1主题 10 - 开始 2线程 6 - 结束 0线程 6 - 结束 2线程 6 - 结束 1按 Enter 退出...

Ideally what I want to do is to delay a task with a non-blocking mode and then wait for all the tasks to complete. I've tried to add the task object returned by Task.Delay and then use Task.WaitAll but seems this won't help. How should I solve this problem?

class Program
{
    public static async void Foo(int num)
    {
        Console.WriteLine("Thread {0} - Start {1}", Thread.CurrentThread.ManagedThreadId, num);

        var newTask = Task.Delay(1000);
        TaskList.Add(newTask);
        await newTask;

        Console.WriteLine("Thread {0} - End {1}", Thread.CurrentThread.ManagedThreadId, num);
    }

    public static List<Task> TaskList = new List<Task>();

    public static void Main(string[] args)
    {
        for (int i = 0; i < 3; i++)
        {
            int idx = i;
            TaskList.Add(Task.Factory.StartNew(() => Foo(idx)));
        }

        Task.WaitAll(TaskList.ToArray());
    }
}

解决方案

Is this what you are trying to achieve?

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        public static async Task Foo(int num)
        {
            Console.WriteLine("Thread {0} - Start {1}", Thread.CurrentThread.ManagedThreadId, num);

            await Task.Delay(1000);

            Console.WriteLine("Thread {0} - End {1}", Thread.CurrentThread.ManagedThreadId, num);
        }

        public static List<Task> TaskList = new List<Task>();

        public static void Main(string[] args)
        {
            for (int i = 0; i < 3; i++)
            {
                int idx = i;
                TaskList.Add(Foo(idx));
            }

            Task.WaitAll(TaskList.ToArray());
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();
        }
    }
}

Output:

Thread 10 - Start 0
Thread 10 - Start 1
Thread 10 - Start 2
Thread 6 - End 0
Thread 6 - End 2
Thread 6 - End 1
Press Enter to exit...

这篇关于使用 Task.WaitAll() 处理等待的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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