Console.WriteLine($"ToastBreadAsync start Current thread {Environment.CurrentManagedThreadId}");
await Task.Delay(3000);
Console.WriteLine($"ToastBreadAsync await 1 Current thread {Environment.CurrentManagedThreadId}");
return new Toast();
}
private static async Task<Egg> FryEggsAsync()
{
Console.WriteLine($"FryEggsAsync start Current thread {Environment.CurrentManagedThreadId}");
await Task.Delay(3000);
Console.WriteLine($"FryEggsAsync await 1 Current thread {Environment.CurrentManagedThreadId}");
await Task.Delay(3000);
Console.WriteLine($"FryEggsAsync await 2 Current thread {Environment.CurrentManagedThreadId}");
return new Egg();
}
public static async Task Main()
{
Console.WriteLine($"Main start Current thread {Environment.CurrentManagedThreadId}");
await Task.WhenAll(ToastBreadAsync(), FryEggsAsync());
Console.WriteLine($"Main end Current thread {Environment.CurrentManagedThreadId}");
}
Пр
имер с msdn. Почему после await thead id становится другим? И как заставить выполняться на одном потоке обе функции?
Main start Current thread 1
ToastBreadAsync start Current thread 1
FryEggsAsync start Current thread 1
FryEggsAsync await 1 Current thread 4
ToastBreadAsync await 1 Current thread 7
FryEggsAsync await 2 Current thread 7
Main end Current thread 7
ограничить шедулер одним потоком
Обсуждают сегодня