无法添加并行任务

Unable to add Parallelism Task

如何为此代码添加任务并行性?我在几个地方添加了但它不是预期的 运行。

class Program
{
    static void Main(string[] args)
    {
        Task.Run(() =>PizzaTask());
    }

    static async void PizzaTask()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        int totalPizza = 10;
        Console.WriteLine($"Started preparing {totalPizza} pizza");
        for (var x = 1; x <= totalPizza; x++)
        {
            //Task.Run(() => MakePizza(x));
            await MakePizza(x);
        }
        stopwatch.Stop();

        Console.WriteLine($"Finished preparing {totalPizza} pizza");
        Console.WriteLine("Elapsed time: " + stopwatch.Elapsed.TotalSeconds);

    }

    static async Task MakePizza(int n)
    {
        PreparePizza(n);
        await BakePizza(n);
    }

    static void PreparePizza(int n)
    {
        Console.WriteLine("Start preparing pizza " + n);
        Thread.Sleep(5000);// synchronous
        Console.WriteLine("Finished preparing pizza " + n);
    }

    static async Task BakePizza(int n)
    {
        Console.WriteLine("Start baking pizza " + n);
        await Task.Delay(15000); // asynchronous 
        //Thread.Sleep(15000); // synchronous 
        Console.WriteLine("Finished baking pizza " + n);
    }
}

之前的工作版本在BakePizza,我设置为async await。但是如果我为 PizzaTask() 设置异步等待,它就不起作用。

我更改了代码,通过 var tasks = Enumerable.Range(0, totalPizza).Select(i => MakePizza(i));

使用并行循环使其异步
  • 下面的代码并行调用 MakePizza 而不是通过 for (var x = 1; x <= totalPizza; x++) { //Task.Run(() => MakePizza(x)); await MakePizza(x); }

  • 顺序调用
  • 使用 Task.Delay 而不是 Thread.Sleep 来确保当前线程未被阻塞。

  • 将代码从 async void 更改为 aysnc Taskasync+void 组合可能会使系统崩溃,通常只应在 UI 端事件处理程序上使用。
  • 如@Steve 所述,对任何可用线程使用ConfigureAwait(false) 到return,而不是将上下文强制return 调用者线程。

更新代码:

public class Program
{
    public static void Main()
    {
       PizzaTask().GetAwaiter().GetResult();
    }

    static async Task PizzaTask()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        int totalPizza = 10;
        Console.WriteLine("Started preparing " + totalPizza + "  pizza");

        var tasks = Enumerable.Range(0, totalPizza).Select(i => MakePizza(i));
        await Task.WhenAll(tasks).ConfigureAwait(false);

        stopwatch.Stop();

        Console.WriteLine("Finished preparing " + totalPizza + "  pizza");
        Console.WriteLine("Elapsed time: " + stopwatch.Elapsed.TotalSeconds);

    }

    static async Task MakePizza(int n)
    {
        await PreparePizza(n).ConfigureAwait(false);
        await BakePizza(n).ConfigureAwait(false);
    }

    static async Task PreparePizza(int n)
    {
        Console.WriteLine("Start preparing pizza " + n);
        await Task.Delay(5000);
        //Thread.Sleep(5000);// synchronous
        Console.WriteLine("Finished preparing pizza " + n);
    }

    static async Task BakePizza(int n)
    {
        Console.WriteLine("Start baking pizza " + n);
        await Task.Delay(15000); // asynchronous 
        //Thread.Sleep(15000); // synchronous 
        Console.WriteLine("Finished baking pizza " + n);
    }
}

您可以在 dotnetfiddle 中检查代码的输出 -- https://dotnetfiddle.net/HepavC