zoukankan      html  css  js  c++  java
  • 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
            }
    

      

      

  • 相关阅读:
    HDU 1874 畅通工程续 (Dijkstra , Floyd , SPFA, Bellman_Ford 四种算法)
    HDU Wooden Sticks
    HDU 3664 Permutation Counting
    javascript DOM添加元素,使用节点属性
    SQL server查询数据类型为ntext是空或NULL值
    TSQL Pivot Tables(行列转换) in SQL Server 2005/2008
    ASP.NET 3.5(c#)区域化设置(LCID)
    Sql 脚本导入EXCEL数据
    asp.net 3.5 csharp 实现事务
    asp.net3.5 csharp: How to show HTML content in calendar tooltip?
  • 原文地址:https://www.cnblogs.com/yaosj/p/10623081.html
Copyright © 2011-2022 走看看