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);
        }
  • 相关阅读:
    redis实现与分析
    NULL, '',0 '0'的区别
    Linux strace命令
    strcpy和memcpy的区别
    图书推荐
    php与mysql通讯那点事
    linux命令汇总
    linux系统信息查询及相关概念
    LNMP zabbix安装
    lftp查看文件时间与登录服务查看文件时间相差8小时
  • 原文地址:https://www.cnblogs.com/chucklu/p/4085057.html
Copyright © 2011-2022 走看看