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

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

问题描述

在理想情况下我想要做的就是延迟任务与非阻塞模式,然后等待所有的任务来完成。我试图通过添加返回Task.Delay任务的对象,然后使用Task.WaitAll,但似乎这也无济于事。我应该怎么解决这个问题呢?

 类节目
{
    公共静态异步无效美孚(INT NUM)
    {
        Console.WriteLine(线程{0} - 启动{1},Thread.CurrentThread.ManagedThreadId,NUM);        VAR newTask = Task.Delay(1000);
        TaskList.Add(newTask);
        等待newTask;        Console.WriteLine(线程{0} - 完{1},Thread.CurrentThread.ManagedThreadId,NUM);
    }    公共静态列表<任务> Tasklist命令=新的List<任务>();    公共静态无效的主要(字串[] args)
    {
        的for(int i = 0;我3;;我++)
        {
            INT IDX = I;
            TaskList.Add(Task.Factory.StartNew(()=>美孚(IDX)));
        }        Task.WaitAll(TaskList.ToArray());
    }
}


解决方案

这是你要实现的目标是什么?

 使用系统;
使用System.Collections.Generic;
使用的System.Threading;
使用System.Threading.Tasks;命名空间ConsoleApplication
{
    类节目
    {
        公共静态异步任务美孚(INT NUM)
        {
            Console.WriteLine(线程{0} - 启动{1},Thread.CurrentThread.ManagedThreadId,NUM);            等待Task.Delay(1000);            Console.WriteLine(线程{0} - 完{1},Thread.CurrentThread.ManagedThreadId,NUM);
        }        公共静态列表<任务> Tasklist命令=新的List<任务>();        公共静态无效的主要(字串[] args)
        {
            的for(int i = 0;我3;;我++)
            {
                INT IDX = I;
                TaskList.Add(美孚(IDX));
            }            Task.WaitAll(TaskList.ToArray());
            Console.WriteLine(preSS Enter键退出...);
            到Console.ReadLine();
        }
    }
}

输出:


螺纹10 - 0开始
螺纹10 - 开始1
螺纹10 - 开始2
螺纹6 - 0结束
螺纹6 - 结束2
螺纹6 - 完1
preSS 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天全站免登陆