zoukankan      html  css  js  c++  java
  • C#基础之Async和Await 的异步编程

    官方文档地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/async/

    Coffee cup = PourCoffee();
    Console.WriteLine("coffee is ready");
    Task<Egg> eggTask = FryEggs(2);
    Task<Bacon> baconTask = FryBacon(3);
    Task<Toast> toastTask = ToastBread(2);
    Toast toast = await toastTask;
    ApplyButter(toast);
    ApplyJam(toast);
    Console.WriteLine("toast is ready");
    Juice oj = PourOJ();
    Console.WriteLine("oj is ready");
    
    Egg eggs = await eggTask;
    Console.WriteLine("eggs are ready");
    Bacon bacon = await baconTask;
    Console.WriteLine("bacon is ready");
    
    Console.WriteLine("Breakfast is ready!");
    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 eggs = await eggsTask;
        Console.WriteLine("eggs are ready");
        var bacon = await baconTask;
        Console.WriteLine("bacon is ready");
        var toast = await toastTask;
        Console.WriteLine("toast is ready");
        Juice oj = PourOJ();
        Console.WriteLine("oj is ready");
    
        Console.WriteLine("Breakfast is ready!");
    
        async Task<Toast> MakeToastWithButterAndJamAsync(int number)
        {
            var toast = await ToastBreadAsync(number);
            ApplyButter(toast);
            ApplyJam(toast);
            return toast;
        }
    }
    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 allTasks = new List<Task>{eggsTask, baconTask, toastTask};
        while (allTasks.Any())
        {
            Task finished = await Task.WhenAny(allTasks);
            if (finished == eggsTask)
            {
                Console.WriteLine("eggs are ready");
            }
            else if (finished == baconTask)
            {
                Console.WriteLine("bacon is 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<Toast> MakeToastWithButterAndJamAsync(int number)
        {
            var toast = await ToastBreadAsync(number);
            ApplyButter(toast);
            ApplyJam(toast);
            return toast;
        }
    }
  • 相关阅读:
    解决 react-native 嵌套路由 warning
    在 function component 中设置 navigationOptions
    《客户端存储技术》阅读笔记
    表单实现原理(Vue ElementUI)
    MVVM 响应式原理(Vue)
    react 实现 loading 动效圈,支持配置转一圈的 duration
    CSS 数字设置等宽
    JSONP(Json with padding)
    javascript this对象
    css盒子模型
  • 原文地址:https://www.cnblogs.com/marshhu/p/11822275.html
Copyright © 2011-2022 走看看