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

【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 的用法详解

http://www.kler.cn/a/574563.html

相关文章:

  • vue实现日历签到效果
  • C# foreach中获取循环索引的4种方式
  • 【机械视觉】C#+visionPro联合编程———【一、C# + VisionPro 联合编程详解以及如何将visionPro工具加载到winform】
  • unity调用本地部署deepseek全流程
  • 函数扩展【ES6】
  • Manus AI 全球首款通用型 Agent,中国制造
  • 极狐GitLab 17.9 正式发布,40+ DevSecOps 重点功能解读【一】
  • MATLAB中startsWith函数用法
  • 面试基础---Redis 延迟队列深度解析
  • ssm_mysql_暖心家装平台
  • 华为OD机试-Excel单元格数值统计(Java 2024 E卷 200分)
  • Mybatis中的分页操作,如何使用PageHelper进行分页,以及Spring Boot整合Mybatis Plus分页
  • SpringBoot读取类路径下文件
  • 【DeepSeek】5分钟快速实现本地化部署教程
  • 【经验分享】Ubuntu20.04编译RK3568 AI模型报错问题(已解决)
  • Java TCP 通信:实现简单的 Echo 服务器与客户端
  • 单片机最小系统原理图设计
  • 【芯片设计】AI偏车载芯片前端设计工程师面试记录·20250304
  • Linux网络编程——TCP并行服务器
  • Swagger UI界面的使用