H-Async Programming Models

Till now we discussed about various threading and synchronization techniques however there are some fundamental design pattern that can be used to solve some typical problems.below are the some well known pattern

Compute Bound Aync Operation

Till this point what we discussed mainly covers compute bound operation. and most of the time we use thread poll directly or indirectly.

IO Bound Async Operations

Microsoft suggest Asynchronous Programming Model APM for all it`s IO operation and provide built in support of them. Any class that provides function like BeginXXX and Readxxx is part of APM implementation.

Event -Based Asynchronous Programming Model (EAP)

Microsoft’s Windows Forms team felt that the IAsyncResult APM was too difficult for many Windows Form developers, so they created a new Event-based Asynchronous Pattern (EAP).11 The main benefit of the EAP is that it integrates with the Microsoft Visual Studio UI designers. That is, you can drag most classes that implement the EAP to a Visual Studio design surface and then double-click event names and have Visual Studio automatically produce the event callback methods and wire the method up to the events themselves.

A well known exapmple of this pattern is Background Worker Thread It is not a suggested pattern for complex application.

APM Vs EAP

    • The biggest benefit of the EPM over the APM is that it can be used with the Visual Studio offering a design-time approach to invoke asynchronous operations.

    • In addition, the EAP was introduced in the FCL at the same time as the SynchronizationContext class, and therefore it has built into it the ability to understand an application’s threading model to ensure that, for GUI applications, the event handler method is invoked on the GUI thread.

    • EAP classes are typically implemented internally using the APM. This means that EAP classes tend to use more memory and perform slower than their APM equivalents.

    • Error handling with the EAP is incongruous with the rest of the system.

    • For simple scenarios, the EPM is easy to use and a fine choice. However, there are some scenarios where the EPM will actually be more complicated to use.

AsyncEnumerator (http://Wintellect.com)

it is a wrapper class over Microsoft AMP classes that solves several complications of AMP development patterns , here is a quick look.

    • It implements the APM itself so it easily integrates with all the Microsoft .NET Framework technologies,

    • It coordinates multiple concurrent asynchronous operations. That is, you can issue many asynchronous operations, and the AsyncEnumerator can notify you after all of them have completed or notify you as each one completes.

    • It supports discard groups, which allow you to issue a set of asynchronous operations and have the AsyncEnumerator object automatically throw away the results as they complete.

    • It allows you to cancel a set of asynchronous operations automatically by calling each operation’s EndXxx methods

    • It frees you from having to worry about your application’s threading model

I would say we must give due consideration to this classes/pattern if planning to use APM in your application

Comparison of Asynchronous Programming Model

This Image is taken from "CLR Via C#"