Click here to Skip to main content
13,450,958 members (55,432 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 6 Jun 2012

An alternative (.NET 2.0) to the Task-based Asynchronous Pattern

, 6 Jun 2012
Rate this:
Please Sign up or sign in to vote.
In this post we will see how to write asynchronous code easily using the AsyncEnumerator class.

Write code that looks like synchronous but actually executes asynchronously with the PowerThreading library and the AsyncEnumerator class.

In my previous post I discussed about the new language features that will help the developers write asynchronous code easily without having to split their flow in callback methods. In this post I want to show you an alternative way of doing this using the AsyncEnumerator class. This works for .NET 2.0+ and it is very similar with the upcoming features.

AsyncEnumerator class resides in the PowerThreading library. It is written by Jeffrey Richter and can be obtained from the Wintellect website.

I will start, again, with some code that executes synchronously: 

 private void FetchStockQuotesSync(WebService svc)
      // This blocks. You don't know when the FetchStockQuotes
      // method returns. It may takes from minutes, to hours or
      // it may not return at all.
      IStockQuote qt = svc.FetchStockQuotes();

Fortunately the WebService class implements the IAsyncResult interface, so the same code can be executed asynchronously using the AsyncEnumerator class:

// Inside your method create an instance of an AsyncEnumerator class,
// specifying the iterator method to be driven by the AsyncEnumerator.
AsyncEnumerator ae = new AsyncEnumerator();
ae.BeginExecute(GetStockQuotesAsyncEnumerator(ae, svc), ae.EndExecute);

private static IEnumerator<Int32> GetStockQuotesAsyncEnumerator(
  AsyncEnumerator ae, WebService svc)
      svc.BeginGetStockQuotes(ae.End(), null);
      yield return 1;
      IStockQuote qt = svc.EndGetStockQuotes(ae.DequeueAsyncResult());

This technique is similar to the new language features for asynchronous programming. In fact, what I've discussed works from .NET 2.0 up to any future version.

A very interesting topic regarding the AsyncEnumerator class and the Async CTP can be found at the PowerTheading discussion group.

Until the Async CTP is shipped you can benefit from what already exists. I have been using the AsyncEnumerator class for quite a long time and I am very happy with it. However, the Async CTP looks promising but it would be very interesting to see some performance measurements in the future.

The sample application can be found here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

BugDuplicate Post Pin
Tim Corey6-Jun-12 4:33
memberTim Corey6-Jun-12 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180318.3 | Last Updated 6 Jun 2012
Article Copyright 2012 by Nikos Baxevanis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid