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);
        }
  • 相关阅读:
    【泛型方法】
    【为什么使用泛型?】
    【泛型类】
    【泛型:ArrayListDemo】
    【泛型概述】
    【Collections:集合工具类:扑克游戏】
    【Collections:集合工具类:自然排序和比较器排序】
    Navicat for MYSQL 断网时本地连接无法打开,2005错误
    Navicat for MYSQL 数据库手动同步方法
    spring redis @Cacheable注解使用部分错误及无效原因
  • 原文地址:https://www.cnblogs.com/chucklu/p/4085057.html
Copyright © 2011-2022 走看看