• C#Async,await异步简单介绍


    C# 5.0 引入了async/await,.net framework4.5开始支持该用法

    使用:

    由async标识的方法必须带有await,如果不带await,方法将被同步执行

            static void Main(string[] args)
            {
                fun22();
                Console.WriteLine($"Main await ThreadId {Thread.CurrentThread.ManagedThreadId}");
                Console.ReadKey();
            }
    
            private async static void fun22()
            {
                //sync同步(相对于调用函数的线程)
                Console.WriteLine($"pre await ThreadId {Thread.CurrentThread.ManagedThreadId}");
                await HelloWorld();
                //async异步(相对于调用函数的线程)
                Console.WriteLine($"after await ThreadId {Thread.CurrentThread.ManagedThreadId}");
            }
    
            private static Task HelloWorld()
            {
                //sync同步(相对于调用函数的线程)
                Console.WriteLine($"pre Task ThreadId {Thread.CurrentThread.ManagedThreadId}");
    
                //async异步(相对于调用函数的线程)
                return Task.Factory.StartNew(() =>
                {
              Thread.Sleep(1000); Console.WriteLine($"Task ThreadId {Thread.CurrentThread.ManagedThreadId}"); }); }

    运行结果:

    结果说明:

    只有执行到Task异步的时候,才使真正的异步 

    微软的例子:

    using System;
    using System.IO;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static async Task Main()
            {
                await ReadCharacters();
            }
    
            static async Task ReadCharacters()
            {
                String result;
                using (StreamReader reader = File.OpenText("existingfile.txt"))
                {
                    Console.WriteLine("Opened file.");
                    result = await reader.ReadToEndAsync();
                    Console.WriteLine("Contains: " + result);
                }
            }
        }
    }
    

      查看ReadToEndAsync源码,发现最后是TaskFactory<Int32>.FromAsyncTrim,真正的异步由这个函数执行,也就是真正的异步是由Task的线程池执行的。

            [HostProtection(ExternalThreading = true)]
            [ComVisible(false)]
            public Task<int> ReadAsync(Byte[] buffer, int offset, int count)
            {
                return ReadAsync(buffer, offset, count, CancellationToken.None);
            }
     
            [HostProtection(ExternalThreading = true)]
            [ComVisible(false)]
            public virtual Task<int> ReadAsync(Byte[] buffer, int offset, int count, CancellationToken cancellationToken)
            {
                // If cancellation was requested, bail early with an already completed task.
                // Otherwise, return a task that represents the Begin/End methods.
                return cancellationToken.IsCancellationRequested
                            ? Task.FromCancellation<int>(cancellationToken)
                            : BeginEndReadAsync(buffer, offset, count);
            }
     
            private Task<Int32> BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
            {            
                return TaskFactory<Int32>.FromAsyncTrim(
                            this, new ReadWriteParameters { Buffer = buffer, Offset = offset, Count = count },
                            (stream, args, callback, state) => stream.BeginRead(args.Buffer, args.Offset, args.Count, callback, state), // cached by compiler
                            (stream, asyncResult) => stream.EndRead(asyncResult)); // cached by compiler
            }
    

      

      

  • 相关阅读:
    今天学到的新知识自己的电脑可以像Github Pages、码云 Pages一样发布静态资源
    移动端安卓开发学习记录Android Studio打断点调试操作步骤记录
    图文并茂学习记录从零开始进行微信小程序开发+引入Vant Weapp组件
    代码小DEMO随笔JS原生手机版本alert弹框
    移动端安卓开发学习记录Android Studio使用adb链接夜神模拟器常用指令
    图文并茂的学习笔记微信小程序自定义tabbar
    解决微信小程序 自定义tabBar 切换时候闪烁问题
    App扫码登录
    TypeScript 参数简化实战(进阶知识点conditional types) 广东靓仔
    前端进阶指南(优秀文章) 广东靓仔
  • 原文地址:https://www.cnblogs.com/yaosj/p/10623081.html
Copyright © 2020-2023  润新知