当前位置: 代码网 > it编程>编程语言>Asp.net > C#中async await异步关键字用法和异步的底层原理全解析

C#中async await异步关键字用法和异步的底层原理全解析

2025年04月17日 Asp.net 我要评论
c#异步编程一、异步编程基础异步编程是啥玩意儿就是让程序在干等着某些耗时操作(比如等网络响应、读写文件啥的)的时候,能把线程腾出来干别的活儿,这样程序就能更灵敏、更高效啦。跟同步编程不一样,同步编程就

c#异步编程

一、异步编程基础

异步编程是啥玩意儿

  • 就是让程序在干等着某些耗时操作(比如等网络响应、读写文件啥的)的时候,能把线程腾出来干别的活儿,这样程序就能更灵敏、更高效啦。
  • 跟同步编程不一样,同步编程就是老老实实等着操作完成才继续往下走,线程就一直被占着,多浪费啊。

异步编程的好处

  • 响应快:比如在做ui界面的时候,用了异步编程,界面就不会卡啦,用户体验贼棒。
  • 省资源:不用让线程一直干等着,资源利用率就上去了。
  • 能扛更多活儿:面对一大堆并发操作的时候,异步编程能轻松搞定,扩展性杠杠滴。

二、异步方法的工作原理

异步方法咋被编译的

  • 你写个async修饰的方法,编译器就把它变成一个状态机啦。
  • 状态机会根据await表达式把方法拆成好多个状态,就跟玩拼图一样。

状态机是咋干活的

  • 状态机就是编译器生成的一个类,它得记着异步方法执行到哪儿了。
  • 核心就是movenext方法,它就像导演一样,指挥着异步操作一步步往下走。
  • 每碰到一个await,就切换一下状态。

await底层是咋实现的

  • await就整出个等待器(awaiter),专门等着异步操作完成。
  • 要是操作还没完,await就记下当前状态,等操作完了再继续往下走。

三、代码示例

httpclient干异步网络请求

  • 弄个httpclient对象,用来发http请求。
  • getstringasync方法,就能异步拿到指定url的网页内容啦。
  • 把拿到的内容打印出来,瞧一瞧成果。
using system;
using system.net.http;
using system.threading.tasks;
namespace asyncawait原理1
{
    class program
    {
        static async task main(string[] args)
        {
            using (httpclient httpclient = new httpclient())
            {
                string html = await httpclient.getstringasync("https://www.baidu.com");
                console.writeline(html);
            }
        }
    }
}

异步读写文件

  • file.writealltextasync方法,能把文本异步写到指定路径的文件里。
  • file.readalltextasync方法,就能把文件内容异步读出来。
  • 把读到的内容打印出来,看看对不对。
using system;
using system.io;
using system.threading.tasks;
namespace asyncawait原理1
{
    class program
    {
        static async task main(string[] args)
        {
            string txt = "hello world";
            string filename = @"e:\temp\1.txt";
            await file.writealltextasync(filename, txt);
            console.writeline("写入成功");
            string s = await file.readalltextasync(filename);
            console.writeline("文件内容:" + s);
        }
    }
}

四、编译后的底层实现

用ilspy反编译dll文件

  • ilspy就是个反编译工具,能把dll文件变回c#代码,方便咱们研究。
  • 把dll文件加载到ilspy里,就能看到编译后的代码啦。
[compilergenerated]
private sealed class <>c__displayclass0_0 : iasyncstatemachine
{
    public int <>1__state;
    public asynctaskmethodbuilder <>t__builder;
    public string[] args;
    private string <>s__1;
    private string <>s__3;
    private string <>s__6;
    private httpclient <httpclient>__4;
    private string <html>__5;
    private string <txt>__2;
    private string <filename>__7;
    private void movenext()
    {
        int num = this.<>1__state;
        try
        {
            taskawaiter<string> awaiter;
            taskawaiter awaiter2;
            switch (num)
            {
                default:
                    this.<httpclient>__4 = new httpclient();
                    goto case 0;
                case 0:
                    try
                    {
                        awaiter = this.<httpclient>__4.getstringasync("https://www.baidu.com").getawaiter();
                        if (!awaiter.iscompleted)
                        {
                            num = this.<>1__state = 0;
                            this.<>t__builder.awaitunsafeoncompleted(ref awaiter, ref this);
                            return;
                        }
                    }
                    catch (exception exception)
                    {
                        this.<>1__state = -2;
                        this.<>t__builder.setexception(exception);
                        return;
                    }
                    this.<html>__5 = awaiter.getresult();
                    console.writeline(this.<html>__5);
                    this.<txt>__2 = "hello yz";
                    this.<filename>__7 = @"e:\temp\1.txt";
                    awaiter2 = file.writealltextasync(this.<filename>__7, this.<txt>__2).getawaiter();
                    if (!awaiter2.iscompleted)
                    {
                        num = this.<>1__state = 1;
                        this.<>t__builder.awaitunsafeoncompleted(ref awaiter2, ref this);
                        return;
                    }
                    break;
                case 1:
                    awaiter2 = this.<>s__1;
                    this.<>s__1 = null;
                    num = this.<>1__state = -1;
                    break;
            }
            awaiter2.getresult();
            console.writeline("写入成功");
            this.<>s__3 = null;
            awaiter = file.readalltextasync(this.<filename>__7).getawaiter();
            if (!awaiter.iscompleted)
            {
                num = this.<>1__state = 2;
                this.<>t__builder.awaitunsafeoncompleted(ref awaiter, ref this);
                return;
            }
            this.<>s__6 = awaiter.getresult();
            console.writeline("文件内容:" + this.<>s__6);
            this.<>s__6 = null;
            this.<>t__builder.setresult();
        }
        catch (exception exception)
        {
            this.<>1__state = -2;
            this.<>t__builder.setexception(exception);
            return;
        }
        this.<>1__state = -1;
    }
    void iasyncstatemachine.movenext()
    {
        // this method is implemented by the compiler-generated code.
    }
    [debuggerhidden]
    private void setstatemachine(iasyncstatemachine statemachine)
    {
        this.<>t__builder.setstatemachine(statemachine);
    }
    void iasyncstatemachine.setstatemachine(iasyncstatemachine statemachine)
    {
        this.setstatemachine(statemachine);
    }
}

看看编译后的状态机代码

  • 分析状态机类的结构,看看都有啥变量、movenext方法长啥样。
  • 瞧瞧awaiter咋用的,状态咋切换的。

理解movenext方法是干啥的

  • movenext就是状态机的发动机,它决定了异步方法咋执行。
  • 在这个方法里,会根据当前状态执行对应的代码,碰到await就暂停,安排好后续咋继续。

五、总结

异步方法编译过程回顾

  • 再唠唠async方法咋被编译成状态机的,状态机又咋根据await拆分方法、驱动异步操作的。

await到底在干啥

  • 说白了,await根本不是真的“等待”,而是靠状态机和等待器来实现的异步协作。
  • 强调一下异步编程的好处,比如响应快、省资源、能扛更多活儿,还有啥场景适合用它。

到此这篇关于c#中async await异步关键字用法和异步的底层原理的文章就介绍到这了,更多相关c# async await异步关键字内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com