zoukankan      html  css  js  c++  java
  • Asynchronous Programming Patterns

    Asynchronous Programming Patterns

    The .NET Framework provides three patterns for performing asynchronous operations:

    1.Asynchronous Programming Model (APM) pattern (also called the IAsyncResult pattern), where asynchronous operations require Begin and End methods (for example, BeginWrite and EndWrite for asynchronous write operations). This pattern is no longer recommended for new development. For more information, see Asynchronous Programming Model (APM)

    2.Event-based Asynchronous Pattern (EAP), which requires a method that has the Async suffix, and also requires one or more events, event handler delegate types, and EventArg-derived types. EAP was introduced in the .NET Framework 2.0. It is no longer recommended for new development. For more information, see Event-based Asynchronous Pattern (EAP).


    3.Task-based Asynchronous Pattern (TAP), which uses a single method to represent the initiation and completion of an asynchronous operation. TAP was introduced in the .NET Framework 4 and is the recommended approach to asynchronous programming in the .NET Framework. The async and await keywords in C# and the Async and Await operators in Visual Basic Language add language support for TAP. For more information, see Task-based Asynchronous Pattern (TAP).

    比较三种异步编程模式

    For a quick comparison of how the three patterns model asynchronous operations, consider a Read method that reads a specified amount of data into a provided buffer starting at a specified offset:

     class MyClass
        {
            /// <summary>
            /// a Read method that reads a specified amount of data into a provided buffer starting at a specified offset
            /// 从buffer字节数组中的第offset位置开始,向后读取count个字节
            /// </summary>
            /// <param name="buffer">源数组数组</param>
            /// <param name="offfset">读取的起始位置(从0开始的偏移量)</param>
            /// <param name="count">读取几个字节</param>
            /// <returns></returns>
            public int Read(byte[] buffer, int offfset, int count)
            {
                return 0;
            }
        }

    APM

        abstract class APM
        {
            //public delegate void AsyncCallback(IAsyncResult ar);    //AsyncCallback是委托
            public abstract IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callBack, object state);
            public abstract int EndRead(IAsyncResult asyncResult);
        }

    EAP

        public delegate void ReadCompletedEventHandler();
        abstract class EAP
        {
            public abstract void ReadAsync(byte[] buffer, int offset, int count);
            public event ReadCompletedEventHandler ReadCompleted;//ReadCompletedEventHandler
        }

    TAP

    abstract class TAP
        {
            public abstract Task<int> ReadAstnc(byte[] buffer, int offset, int count);
        }
  • 相关阅读:
    阿里云高级技术专家周晶:基于融合与协同的边缘云原生体系实践
    Spring Boot Serverless 实战系列“架构篇” 首发 | 光速入门函数计算
    基于 EMR OLAP 的开源实时数仓解决方案之 ClickHouse 事务实现
    【ClickHouse 技术系列】 在 ClickHouse 中处理实时更新
    LeetCode_Two Sum
    LeetCode_ Remove Element
    LeetCode_Same Tree
    LeetCode_Symmetric Tree
    LeetCode_Path Sum
    LeetCode_Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/chucklu/p/4085057.html
Copyright © 2011-2022 走看看