当前位置: 首页 > news >正文

【C#】async与await介绍

1. 实例1

1.1 代码

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Method1();
            Method2();
            Console.ReadKey();
        }

        public static async Task Method1()
        {
            Console.WriteLine("Method 1 Start................");

            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Method 1");
                }
            });

            //await的Task运行完以后,下面的代码才运行
            Console.WriteLine("Method 1 End................");
        }

        public static void Method2()
        {
            for (int i = 0; i < 25; i++)
            {
                Console.WriteLine(" Method 2");
            }
        }
    }
}

1.2 运行结果

  • Method2不会等待Method1运行结束再运行。
  • async方法中,await后面的代码也要等待await运行结束以后再运行。
    在这里插入图片描述
    在这里插入图片描述

2. 实例2

2.1 代码

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            callMethod();
            Console.ReadKey();
        }

        public static async void callMethod()
        {
            //方式1,Method3需要等待Method1的结果,所以Method1与Method2交替执行,Method3等Method1执行完毕再执行
            //await之后的需要等待前面的运行完再运行。
            Task<int> task = Method1();
            Method2();
            int count = await task;
            Method3(count);

            //方式2,顺序执行,运行完Method1再运行Method2再运行Method3
            //int count = await Method1();
            //Method2();
            //Method3(count);

            //方式3,报错。必须加await
            //int count = Method1();
            //Method2();
            //Method3(count);

            //方式4,Method1与[Method2,Method3]交替运行,Method2和Method3顺序执行
            //Method1();
            //Method2();
            //Method3(4);
        }

        public static async Task<int> Method1()
        {
            int count = 0;
            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Method 1");
                    count += 1;
                }
            });
            return count;
        }

        public static void Method2()
        {
            for (int i = 0; i < 25; i++)
            {
                Console.WriteLine(" Method 2");
            }
        }

        public static void Method3(int count)
        {
            Console.WriteLine(" Method 3 Total count is " + count);
        }
    }
}

2.2 运行结果

  • 方式1的运行结果
    在这里插入图片描述

3. 总结

  • 调用async方法且不使用await修饰,不阻塞,直接运行。
  • 调用async方法且使用await修饰,阻塞等待,直到运行完成再运行后面的代码。

参考:

  • 深入解析C#中的async和await关键字
  • C# 中的Async 和 Await 的用法详解

相关文章:

  • JAVASE(一)
  • 计算机视觉|3D卷积网络VoxelNet:点云检测的革新力量
  • Unity组件TrailRenderer屏幕滑动拖尾
  • 策略模式详解:实现灵活多样的支付方式
  • 数据上新 | 专业领域多语种对话语音数据集
  • P63 C++当中的计时
  • 不小心更改了/etc权限为777导致sudo,ssh等软件都无法使用
  • “此电脑”中删除WPS云盘方法(百度网盘通用)
  • 10 【HarmonyOS NEXT】 仿uv-ui组件开发之Avatar头像组件开发教程(一)
  • 为何吹订单?因为特斯拉的销量已遥遥领先,掩耳盗铃之举!
  • 第10章 metasploit(网络安全防御实战--蓝军武器库)
  • 每日一题----------异常处理
  • 【Python运维】实现高效的自动化备份与恢复:Python脚本从入门到实践
  • 数据结构---八大排序
  • Python的学习篇(七)--网页结构
  • 数据清洗与治理:为大模型预训练打造完美数据
  • Python已知后序遍历和中序遍历,求先序遍历
  • 字典树(trie树)详解
  • TCP/IP 5层协议簇:网络层(ICMP协议)
  • 如何学习编程?
  • 北京和君网站建设/今天重大新闻事件
  • 拱墅网站建设/百度首页 百度
  • 高端大气上档次的网站模板/怎么推广软件
  • o2o商城上的二级网站/上海网站建设推广服务
  • 营销型企业网站诊断/seo技术教学视频
  • 免费创办网站/百度百科官网登录