12

Recently I stumbled across the AsyncEnumerator class form Jeffrey Richter's Power Threading Library which seems to solve several problems I'm usually encountering when programming asynchronous stuff.

The idea for this class has been around for quite a while now and I'm wondering if current versions of .NET / C# have built-in support for this mechanism by now or if it is still necessary to rely on a third party library? Or maybe newer versions of .NET have some alternative approach which simplifies asynchronous programming as much as Richter's AsyncEnumerator does?

Or in other words: Is there a reason to not start using Richter's AsyncEnumerator today?

Edit:

Some links with information on AsyncEnumerator:

4 Answers 4

19

Yes, you will still benefit from my AsyncEnumerator. The new threading stuff introduced in .NET 4 (Tasks, Parallel, PLINQ, etc), is all about concurrency. That is, they are all about taking a computational workload, dividing it up and spawning it out onto multiple threads so that the workload can complete in less time than it would takes 1 thread to do the entire workload. You can use these constructs to execute multiple synchronous I/O operations concurrently. However, the synchronous I/O operations cause all the threads to block which then causes the threadpool to create more threads. And so, your app's resource usage skyrockets while your CPU usage remains very low. This is a very inefficient to implement your application and prevents your app from scaling.

My AsyncEnumerator is all about initiating asynchronous I/O operations without blocking any threads so that your app's resource usage remains very low so your app scales very well. CPU usage remains low in this case too because you are performing I/O; not a computational workload.

In the next version of .NET, the new async/await language features (which I worked on with Microsoft), enables you to perform async I/O and in fact, the new features are modeled very similar to my AsyncEnumerator. So much so that you can port code that uses my AsyncEnumerator to the new model with very few source code changes.

As someone else pointed out, my AsyncEnumerator still offers other features and works with .NET 2.0 and later. So, many people will still find it quite useful for a long time.

3
  • Thanks for your explanations! I started to use your library and I'm quite happy with it. However I'm not sure about how to implement exception handling correctly. Maybe you could have a look at my other question here. Thanks a lot! Aug 19, 2011 at 13:27
  • Hi Jeff, can we get an update on this answer, especially given that we have Task.Factory.FromAsync, async and await additions to the framework/language since you wrote this answer?
    – Aron
    Jul 15, 2019 at 7:21
  • 2
    The whole async/await stuff came out of my AsyncEnumerator work so the async/await mechanism is the "new" way of accomplishing the same thing as my AsyncEnumerator. Jul 16, 2019 at 15:14
9

An obvious parallel here is PLINQ, but Richter himself dismisses this:

Jeffrey Richter Dec 04, 2008 at 2:27 PMquotereply They are very different. Parallel.For in particular is about performing a bunch of compute-bound operations in parallel scaling out across all the CPUs in teh machine. My AsyncEnumerator is mostly about issuing one or more concurrent I/O-bound operations without have any threads block for them to complete.

However, the C# async CTP may be useful here, making thread continuations much more reasonable, i.e.

var bytesRead = await stream.ReadAsync(...);
Console.WriteLine(bytesRead); // woah! we're on a different thread now!

Here, the C# compiler re-writes everything around await instuctions, such that it becomes a callback/continuation to the existing async operation (which must return an "awaitable" value). Once this is in production, I hope this will become a much more natural way to write code with intrinsic delays due to async.

5
  • From what I've seen about PLINQ I also don't think it is an obvious parallel. What I like about AsyncEnumerator is that you can express a sequence of asynchronous operations in a very natural way (like connecting to a web server, request some data, based on the result request some other data). An it all looks like a synchronous program. On top of that you have support for cancellation/timeouts and you don't have to worry about marshalling to the UI thread. I don't see how I could handle something like this with PLINQ. Aug 18, 2011 at 12:48
  • @Robert which is exactly what await does, too - in regular-looking C#. var response = await GetResponse(); etc Aug 18, 2011 at 12:50
  • Regarding the await keyword... I'd like to stick with what's currently available (I'm writing production code) Aug 18, 2011 at 12:50
  • @Robert well, even in current code, the TPL might be useful (ContinueWith etc). Not as elegant as await, but gets the job done. Aug 18, 2011 at 12:51
  • Yes it looks like TPL could be a real alternative. I guess I have to read more about TPL first; but so far I still tend to like Richter's approach most. His examples just look so clean and simple; and it solves so many problems at once. And it even seems to work with older versions of the framework. Aug 18, 2011 at 13:01
2

.net 4.0 includes the PLINQ framework and various other means of threaded computation.

See Parallel Programming in the .NET Framework for more details.

0

From this question Asynchronous iterator Task<IEnumerable<T>> :

It sounds like what you may really be looking for is something like IObservable<T>, which is sort of like a push-based asynchronous IEnumerable<T>. See Reactive Extensions, a.k.a. Rx from Microsoft Open Technologies (code licensed under Apache-2.0) (no affiliation) for a huge host of methods that work with IObservable<T> to make it work like LINQ-to-Objects and more.

The problem with IEnumerable<T> is that there's nothing that really makes the enumeration itself asynchronous. If you don't want to add a dependency on Rx (which is really what makes IObservable<T> shine), this alternative might work for you:

public async Task<IEnumerable<char>> TestAsync(string testString)
{
    return GetChars(testString);
}

private static IEnumerable<char> GetChars(string testString)
{
    foreach (char c in testString.ToCharArray())
    {
        // do other work
        yield return c;
    }
}

though I'd like to point out that without knowing what's **actually being done asynchronously, there may be a much better way to accomplish your goals. None of the code you posted will actually do anything asynchronously, and I don't really know if anything in // do other work is asynchronous (in which case, this isn't a solution to your underlying problem though it will make your code compile).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.