首页 > 其他分享 >WhenAll()和WhenAny()

WhenAll()和WhenAny()

时间:2023-12-26 23:24:22浏览次数:39  
标签:toast Task Console WhenAny static WriteLine var WhenAll

1.概念

WhenAll:接收一个Task数组作为参数,返回一个新的Task,该Task会在所有传入的Task都完成后完成。

WhenAny:接受一个Task数组作为参数,返回一个新的Task,该Task会在任意一个传入的Task完成后完成。

基本使用:

static async Task Main(string[] args)
{
    int failed = 0;
    var tasks = new List<Task>();
    String[] urls =
        { "www.adatum.com", "www.cohovineyard.com",
        "www.cohowinery.com", "www.northwindtraders.com",
        "www.contoso.com" };

    foreach (var value in urls)
    {
        var url = value;
        tasks.Add(Task.Run(() =>
        {
            var png = new Ping();
            try
            {
                var reply = png.Send(url);//对数组中的 URL 执行 ping 操作
                if (!(reply.Status == IPStatus.Success))
                {
                    Interlocked.Increment(ref failed);
                    throw new TimeoutException("Unable to reach " + url + ".");
                }
            }
            catch (PingException)
            {
                Interlocked.Increment(ref failed);
                throw;
            }
        }));
    }
    Task t = Task.WhenAll(tasks);
    await t;
    if (t.Status == TaskStatus.RanToCompletion)//以确定是否有任何任务出错。
        Console.WriteLine("All ping attempts succeeded.");
    else if (t.Status == TaskStatus.Faulted)
        Console.WriteLine("{0} ping attempts failed", failed);
}

//结果:All ping attempts succeeded.

 

2.复杂应用

异步制作早餐场景:参考文档

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AsyncBreakfast
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Coffee cup = PourCoffee();
            Console.WriteLine("coffee is ready");

            var eggsTask = FryEggsAsync(2);
            var baconTask = FryBaconAsync(3);
            var toastTask = MakeToastWithButterAndJamAsync(2);

            var breakfastTasks = new List<Task> { eggsTask, baconTask, toastTask };
            while (breakfastTasks.Count > 0)
            {
                Task finishedTask = await Task.WhenAny(breakfastTasks);
                Console.WriteLine("等待WhenAny返回的第一个已完成的Task");
                if (finishedTask == eggsTask)
                {
                    Console.WriteLine("eggs are ready");
                }
                else if (finishedTask == baconTask)
                {
                    Console.WriteLine("bacon is ready");
                }
                else if (finishedTask == toastTask)
                {
                    Console.WriteLine("toast is ready");
                }
                //您应该再次等待该任务,即使您知道它已经完成运行。这就是如何检索它的结果,或者确保引发错误的异常被抛出。
                await finishedTask;
                breakfastTasks.Remove(finishedTask);
            }

            Juice oj = PourOJ();
            Console.WriteLine("oj is ready");
            Console.WriteLine("Breakfast is ready!");
        }

        static async Task<Toast> MakeToastWithButterAndJamAsync(int number)
        {
            var toast = await ToastBreadAsync(number);
            ApplyButter(toast);
            ApplyJam(toast);

            return toast;
        }

        private static Juice PourOJ()
        {
            Console.WriteLine("Pouring orange juice");
            return new Juice();
        }

        private static void ApplyJam(Toast toast) =>
            Console.WriteLine("Putting jam on the toast");

        private static void ApplyButter(Toast toast) =>
            Console.WriteLine("Putting butter on the toast");

        private static async Task<Toast> ToastBreadAsync(int slices)
        {
            for (int slice = 0; slice < slices; slice++)
            {
                Console.WriteLine("Putting a slice of bread in the toaster");
            }
            Console.WriteLine("Start toasting...");
            await Task.Delay(3000);
            Console.WriteLine("Remove toast from toaster");

            return new Toast();
        }

        private static async Task<Bacon> FryBaconAsync(int slices)
        {
            Console.WriteLine($"putting {slices} slices of bacon in the pan");
            Console.WriteLine("cooking first side of bacon...");
            await Task.Delay(3000);
            for (int slice = 0; slice < slices; slice++)
            {
                Console.WriteLine("flipping a slice of bacon");
            }
            Console.WriteLine("cooking the second side of bacon...");
            await Task.Delay(3000);
            Console.WriteLine("Put bacon on plate");

            return new Bacon();
        }

        private static async Task<Egg> FryEggsAsync(int howMany)
        {
            Console.WriteLine("Warming the egg pan...");
            await Task.Delay(3000);
            Console.WriteLine($"cracking {howMany} eggs");
            Console.WriteLine("cooking the eggs ...");
            await Task.Delay(3000);
            Console.WriteLine("Put eggs on plate");

            return new Egg();
        }

        private static Coffee PourCoffee()
        {
            Console.WriteLine("Pouring coffee");
            return new Coffee();
        }
    }
    // These classes are intentionally empty for the purpose of this example. They are simply marker classes for the purpose of demonstration, contain no properties, and serve no other purpose.
    internal class Bacon { }
    internal class Coffee { }
    internal class Egg { }
    internal class Juice { }
    internal class Toast { }

}

 

标签:toast,Task,Console,WhenAny,static,WriteLine,var,WhenAll
From: https://www.cnblogs.com/xixi-in-summer/p/17929583.html

相关文章

  • 异步编程WhenAll和WaitAll
    如果有多个Task任务,如果需要等待所有任务执行完成使用:WaitAll或WhenAll如果只需要等待其中一个执行完成则使用WaitAny或WhenAnyWhen表示有返回值Wait表示无返回值staticasyncTaskMain(string[]args){Console.WriteLine("无返回值开始");Task.WaitAll(NoResult(......
  • 关于.net4.0使用WhenAny实现Task超时机制
    .net4.0想要使用await/async语法糖必须要引用:Microsoft.BclMicrosoft.Bcl.Async Microsoft.Bcl.Build可以从nuget引用此三个包publicstaticasyncTask<TResult>TryRunWithTimeoutAsync<TResult>(thisFunc<TResult>function,intdueTime){//......
  • C#异步方法中Task.WhenAll的使用
    一、说明Task.WhenAll()、Task.WhenAny()这两个与Task.WaitALL()、Task.WaitAny()是有区别的,When是异步的,Wait是同步的。Task.WhenAll():所有task完成时,task才完成,用于等待多个任务执行结束。Task.WhenAny():任何一个task完成时,task完成。二、示例:usingSystem;usingSystem.IO......
  • C#异步方法中Task.WhenAll的使用
    一、说明Task.WhenAll()、Task.WhenAny()这两个与Task.WaitALL()、Task.WaitAny()是有区别的,When是异步的,Wait是同步的。Task.WhenAll():所有task完成时,task才完成,用于等待......
  • 第十八节:异步补充(CancellationToken终止信号、whenAll、whenAny、yeild return的用法
    一.CancellationToken终止信号 CancelAfter()超时后发出取消信号 Cancel()发出取消信号{CancellationTokenSourcects=new();cts.CancelAfter(4000);......
  • C# 异步编程 Task.WhenAll()
    Task<string>t1=File.ReadAllTextAsync("d:/1.txt");Task<string>t2=File.ReadAllTextAsync("d:/2.txt");Task<string>t3=File.ReadAllTextAsync("d:/3.txt");//等待......