Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Asynchronous programming patterns

  • 2023-02-13
Feedback

In this article

.NET provides three patterns for performing asynchronous operations:

  • Task-based Asynchronous Pattern (TAP), which uses a single method to represent the initiation and completion of an asynchronous operation. TAP was introduced in .NET Framework 4.It's the recommended approach to asynchronous programming in .NET. Theasync andawait keywords in C# and theAsync andAwait operators in Visual Basic add language support for TAP. For more information, seeTask-based Asynchronous Pattern (TAP).

  • Event-based Asynchronous Pattern (EAP), which is the event-based legacy model for providing asynchronous behavior. It requires a method that has theAsync suffix and one or more events, event handler delegate types, andEventArg-derived types. EAP was introduced in .NET Framework 2.0. It's no longer recommended for new development. For more information, seeEvent-based Asynchronous Pattern (EAP).

  • Asynchronous Programming Model (APM) pattern (also called theIAsyncResult pattern), which is the legacy model that uses theIAsyncResult interface to provide asynchronous behavior. In this pattern, asynchronous operations requireBegin andEnd methods (for example,BeginWrite andEndWrite to implement an asynchronous write operation). This pattern is no longer recommended for new development. For more information, seeAsynchronous Programming Model (APM).

Comparison of patterns

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

public class MyClass  {      public int Read(byte [] buffer, int offset, int count);  }

The TAP counterpart of this method would expose the following singleReadAsync method:

public class MyClass  {      public Task<int> ReadAsync(byte [] buffer, int offset, int count);  }

The EAP counterpart would expose the following set of types and members:

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

The APM counterpart would expose theBeginRead andEndRead methods:

public class MyClass  {      public IAsyncResult BeginRead(          byte [] buffer, int offset, int count,        AsyncCallback callback, object state);      public int EndRead(IAsyncResult asyncResult);  }

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo