Asynchronous Programming Patterns

本文介绍了.NET Framework提供的三种异步编程模式:APM模式、EAP模式及推荐的TAP模式,并通过示例对比了这些模式如何实现异步操作。

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);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值