转换使用Thread的函数以使用Async / Await

时间:2017-10-04 14:40:20

标签: c# multithreading asynchronous async-await

是否可以使用c#中的新Async / Await功能执行以下操作:

.svglogo {
  height:70px !important;
}

我知道Async / Await存在限制,你只能使用线程。但是对于像这样的例子,有一个函数DoWork需要同时在多个线程上运行,可以这样做吗?

1 个答案:

答案 0 :(得分:-1)

此?

    static void Main(string[] args)
    {
        Task[] tasks = new Task[101];

        for (int i = 0; i < tasks.Length; i++)
        {
            tasks[i] = IntenseWork();
        }
        Task.WaitAll(tasks);
        Console.WriteLine("Press Any Key to Continue...");
        Console.ReadKey();
    }

    private static async Task IntenseWork()
    {
        await Task.Delay(5000);
    }