Hello,
Is good for understanding. Does anyone have the complete example?
Is good for understanding. Does anyone have the complete example?
I use a class for that.
await Task.WhenAll(eggsTask, baconTask, toastTask); await Task.Any(eggsTask, baconTask, toastTask); or with ManualResetEvent namespace TaskThreadAwait01 { class Program { // https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/concepts/async/ public class BaseCoffee { } public class PourCoffee : BaseCoffee { } public class Toast { } public class Eggs { } public class Working { public async Task Test() { PourCoffee cup = new PourCoffee(); Console.WriteLine("coffee is ready"); var eggsTask = FryEggsAsync(12); var toastTask = MakeToastWithButterAndJamAsync(20); var allTasks = new List<Task> { toastTask }; while (allTasks.Any()) { Task finished = await Task.WhenAny(allTasks); if (finished == eggsTask) { Console.WriteLine("eggs are ready"); } else if (finished == toastTask) { Console.WriteLine("toast is ready"); } allTasks.Remove(finished); } //Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready!"); } async Task<Eggs> FryEggsAsync(int number) { Eggs myEggs = new Eggs(); await Task.Delay(number * 1000); return myEggs; } async Task<Toast> ToastBreadAsync(int number) { Toast myToast = new Toast(); await Task.Delay(number * 1000); return myToast; } async Task<Toast> MakeToastWithButterAndJamAsync(int number) { var toast = await ToastBreadAsync(number); ApplyButter(toast); //ApplyJam(toast); return toast; } public void ApplyButter(Toast toast) { // await Task.Delay(number * 1000); } } static void Main(string[] args) { Working t1 = new Working(); t1.Test().Wait(); } }
Greetings Markus