【C#】async与await介绍

1. 实例1

1.1 代码

csharp 复制代码
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 代码

csharp 复制代码
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修饰,阻塞等待,直到运行完成再运行后面的代码。

参考:

相关推荐
counting money12 分钟前
JAVA泛型基础
java·开发语言·eclipse
007php00715 分钟前
Go语言面试:传值与传引用的区别及选择指南
java·开发语言·后端·算法·面试·golang·xcode
XerCis19 分钟前
Python的RSS/Atom源解析库feedparser
开发语言·python
algonaut19 分钟前
adobe acrobat 安装到使用再到PDF编辑【适合小白,只看一篇就够!!!】
java·开发语言·其他·pdf
boonya36 分钟前
Java JVM核心原理与面试题解析
java·开发语言·jvm
魔都吴所谓1 小时前
【Python】根据开始时间、结束时间计算中间时间
开发语言·windows·python
Sammyyyyy1 小时前
Node.js 做 Web 后端优势为什么这么大?
开发语言·前端·javascript·后端·node.js·servbay
27^×2 小时前
Java 内存模型与垃圾回收机制详解
java·开发语言
max5006002 小时前
本地部署开源数据生成器项目实战指南
开发语言·人工智能·python·深度学习·算法·开源