为什么我的第一个任务不会使用 await 关键字等待

Why won't my 1st Task wait using the await keyword

尝试使用 C# (.Net6) 中的任务模拟泡茶。我的问题是我希望 BoilWater &(GetCup、InsertTeaBag 和 GetMilk)同步到 运行,然后在它们完成后,其余方法是 运行。我使用了 await 关键字,但它不起作用。

我的代码:

await MakeTea();

async Task MakeTea()
{
    await Task.Run(() => {

        Task.Run(() => BoilWater());
        Task.Run(() =>
        {
            GetCup();
            InsertTeabag();
            GetMilk();
        });

    });

    PourBoiledWater();
    StirTeaBag();
    PourMilk();
    FinalStir();
}

async Task BoilWater()
{
    Console.WriteLine("boiling water...");
    Thread.Sleep(20000);
    Console.WriteLine("finished boiling water!!!");
    Console.WriteLine("-----------------------");
}

async Task InsertTeabag()
{
    Console.WriteLine("taking out tea bag box out of cupboard and insert it into cup...");
    Thread.Sleep(5000);
    Console.WriteLine("finished inserting teabag!!!");
    Console.WriteLine("-----------------------");
}

async Task GetCup()
{
    Console.WriteLine("taking out tea cup out of cupboard...");
    Thread.Sleep(2000);
    Console.WriteLine("finished taking out cup!!!");
    Console.WriteLine("-----------------------");
}

async Task GetMilk()
{
    Console.WriteLine("taking out milk out of the fridge...");
    Thread.Sleep(4000);
    Console.WriteLine("finished taking getting milk!!!");
    Console.WriteLine("-----------------------");
}

async Task PourMilk()
{
    Console.WriteLine("pouring milk...");
    Thread.Sleep(3000);
    Console.WriteLine("finished pouring milk!!!");
    Console.WriteLine("-----------------------");
}

async Task PourBoiledWater()
{
    Console.WriteLine("pouring boiling water...");
    Thread.Sleep(3000);
    Console.WriteLine("finished pouring boiling water!!!");
    Console.WriteLine("-----------------------");
}

async Task StirTeaBag()
{
    Console.WriteLine("stirring teabag with boiled water...");
    Thread.Sleep(9000);
    Console.WriteLine("finihsed stirring!!!");
    Console.WriteLine("-----------------------");
}

async Task FinalStir()
{
    Console.WriteLine("stirring milk into tea...");
    Thread.Sleep(2000);
    Console.WriteLine("finished stirring!!!");
    Console.WriteLine("-----------------------");
}

预先感谢您的帮助。

输出:

taking out tea cup out of cupboard...
pouring boiling water...
boiling water...
finished taking out cup!!!
-----------------------
taking out tea bag box out of cupboard and insert it into cup...
finished pouring boiling water!!!
-----------------------
stirring teabag with boiled water...
finished inserting teabag!!!
-----------------------
taking out milk out of the fridge...
finished taking getting milk!!!
-----------------------
finihsed stirring!!!
-----------------------
pouring milk...
finished pouring milk!!!
-----------------------
stirring milk into tea...
finished stirring!!!
-----------------------

await 总是关于在每个级别上链接异步。一起使用 asyncawait。你以错误的方式链接它

await Task.Run(() => { // returned task only start other tasks and don't wait for their end.
    Task.Run(() => BoilWater());
    Task.Run(() =>
        {
            GetCup();
            InsertTeabag();
            GetMilk();
        });
}); 

你需要:

var boilTask = Task.Run(async () => await BoilWater());
var otherPreparationsTask = Task.Run(async () =>
                {
                    await GetCup();
                    await InsertTeabag();
                    await GetMilk();
                }); // you will get only one task for all this nethods. After all of them get completed consequently

await Task.WhenAll(new[] {boilTask, otherPreparationsTask});

并且您不得将 Sleepasync 关键字一起使用。你必须使用

await Task.Delay(...)

这是我的输出和源代码:

boiling water...
taking out tea cup out of cupboard...
finished taking out cup!!!
-----------------------
taking out tea bag box out of cupboard and insert it into cup...
finished inserting teabag!!!
-----------------------
taking out milk out of the fridge...
finished taking getting milk!!!
-----------------------
finished boiling water!!!
-----------------------
pouring boiling water...
finished pouring boiling water!!!
-----------------------
stirring teabag with boiled water...
finihsed stirring!!!
-----------------------
pouring milk...
finished pouring milk!!!
-----------------------
stirring milk into tea...
finished stirring!!!
-----------------------

await MakeTea();
    
async Task MakeTea()
{
    var boilTask = Task.Run(async () => await BoilWater());
    var otherTasks = Task.Run(async () =>
            {
                await GetCup();
                await InsertTeabag();
                await GetMilk();
            });

    await Task.WhenAll(new[] {boilTask, otherTasks});

    await PourBoiledWater();
    await StirTeaBag();
    await PourMilk();
    await FinalStir();
}
    
async Task BoilWater()
{
    Console.WriteLine("boiling water...");
    await Task.Delay(20000);
    Console.WriteLine("finished boiling water!!!");
    Console.WriteLine("-----------------------");
}
    
async Task InsertTeabag()
{
    Console.WriteLine("taking out tea bag box out of cupboard and insert it into cup...");
    await Task.Delay(5000);
    Console.WriteLine("finished inserting teabag!!!");
    Console.WriteLine("-----------------------");
}
    
async Task GetCup()
{
    Console.WriteLine("taking out tea cup out of cupboard...");
    await Task.Delay(2000);
    Console.WriteLine("finished taking out cup!!!");
    Console.WriteLine("-----------------------");
}
    
async Task GetMilk()
{
    Console.WriteLine("taking out milk out of the fridge...");
    await Task.Delay(4000);
    Console.WriteLine("finished taking getting milk!!!");
    Console.WriteLine("-----------------------");
}
    
async Task PourMilk()
{
    Console.WriteLine("pouring milk...");
    await Task.Delay(3000);
    Console.WriteLine("finished pouring milk!!!");
    Console.WriteLine("-----------------------");
}
    
async Task PourBoiledWater()
{
    Console.WriteLine("pouring boiling water...");
    await Task.Delay(3000);
    Console.WriteLine("finished pouring boiling water!!!");
    Console.WriteLine("-----------------------");
}
    
async Task StirTeaBag()
{
    Console.WriteLine("stirring teabag with boiled water...");
    await Task.Delay(9000);
    Console.WriteLine("finihsed stirring!!!");
    Console.WriteLine("-----------------------");
}
    
async Task FinalStir()
{
    Console.WriteLine("stirring milk into tea...");
    await Task.Delay(2000);
    Console.WriteLine("finished stirring!!!");
    Console.WriteLine("-----------------------");
}

使用await Task.Delay(int)代替Thread.Sleep(int) 另外,您没有为 t1 和 t2 执行全部等待。 t1 和 t2 完成后,您必须执行另一项任务,因此您需要等待使用 WhenAll() 或 WaitAll();

await MakeTea();

async Task MakeTea()
{
    await Task.Run(async () => {
        

        Task t1= Task.Run(async() => await BoilWater());
        Task t2= Task.Run(async() =>
        {
            await GetCup();
            await InsertTeabag();
            await GetMilk();
        });

        await Task.WhenAll(t1,t2);
    });

   await PourBoiledWater();
   await StirTeaBag();
   await PourMilk();
   await FinalStir();
}

async Task BoilWater()
{
    Console.WriteLine("boiling water...");
    await Task.Delay(20000);
    Console.WriteLine("finished boiling water!!!");
    Console.WriteLine("-----------------------");
}

async Task InsertTeabag()
{
    Console.WriteLine("taking out tea bag box out of cupboard and insert it into cup...");
    await Task.Delay(5000);
    Console.WriteLine("finished inserting teabag!!!");
    Console.WriteLine("-----------------------");
}

async Task GetCup()
{
    Console.WriteLine("taking out tea cup out of cupboard...");
     await Task.Delay(2000);
    Console.WriteLine("finished taking out cup!!!");
    Console.WriteLine("-----------------------");
}

async Task GetMilk()
{
    Console.WriteLine("taking out milk out of the fridge...");
    await Task.Delay(4000);
    Console.WriteLine("finished taking getting milk!!!");
    Console.WriteLine("-----------------------");
}

async Task PourMilk()
{
    Console.WriteLine("pouring milk...");
    await Task.Delay(3000);
    Console.WriteLine("finished pouring milk!!!");
    Console.WriteLine("-----------------------");
}

async Task PourBoiledWater()
{
    Console.WriteLine("pouring boiling water...");
    await Task.Delay(3000);
    Console.WriteLine("finished pouring boiling water!!!");
    Console.WriteLine("-----------------------");
}

async Task StirTeaBag()
{
    Console.WriteLine("stirring teabag with boiled water...");
    await Task.Delay(9000);
    Console.WriteLine("finihsed stirring!!!");
    Console.WriteLine("-----------------------");
}

async Task FinalStir()
{
    Console.WriteLine("stirring milk into tea...");
    await Task.Delay(2000);
    Console.WriteLine("finished stirring!!!");
    Console.WriteLine("-----------------------");
}