这是我试图模拟的代码。我可以删除 async-await 来执行“即发即忘”,但我试图理解异步(非阻塞)代码的目的,但看起来 AWAIT 是运行该方法之前的阻塞代码NextProcess()。如果我删除等待,它就满足了我的目标,但是在后台运行完成后有没有办法获得任何类型的异常或状态?
public static async Task Main()
{
Console.WriteLine("Starting the long-running process...");
await Task.Run(() => LongRunningProcess());
NextProcess();
}
private static void LongRunningProcess()
{
// Simulating the work being done
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
private static void NextProcess()
{
Console.WriteLine("In See method, continuation without waiting!");
}
Run Code Online (Sandbox Code Playgroud)
我尝试了粘贴代码的各种排列。