Jeff Richter Video on Asynchronous Programming and his Power Threading Library
I recently had the chance to sit down with Jeff Richter and discuss his Power Threading Library. This library provides a simple technique for handling asynchronous development. By making clever use of C# Iterators, Jeff is able to make asynchronous code looks as though it is synchronous code the executes in a linear fashion. Jeff's library greatly simplifies the asynchronous programming model, making it easy for you to navigate waters that you may have once considered formidably perilous.
Figure 1: Jeff Richter explains how to use his Power Threading Library.
Many of you who've had experience with asynchronous development know how difficult such code can be to write. Asynchronous code is typically non-linear, and jumps from one portion of a program to another. It is difficult to debug, and is difficult to tame if errors occur.
To understand the difficulties inherent in asynchronous development it helps to first consider a simple example. Suppose you begin an IO operation of some kind, perhaps the download of a large file. The download is going to take several minutes. To avoid locking up your program during the download, you set up a thread on which the operation can run. You start the thread, call it from your main thread, and set up a callback method which can be executed when the operation completes. Because the download is run on a secondary thread, the main thread of your program is still responsive during the download, and can interact with the user. When the task completes, the callback is executed, thereby announcing the termination of the download. You might then have a new asynchronous task that you might want to begin, such as processing the downloaded file and adding portions of its content to a database. Again, this task is going to take some time, and so you start another thread, providing another callback method that can be executed when the task is completed.
The model outlined in the previous paragraph is common, but awkward. The problems inherent in this scenario are numerous, but two of the worst problems can be summed up as follows:
- The code does not execute in serial fashion, but instead jumps from one callback to another, thereby making it difficult to debug. Someone new to the code might find it hard to understand which callback will execute next, or which thread is currently active.
- If something goes wrong during the execution of the code, it can be very difficult to clean up the current operation and exit the process smoothly. Operations are occurring on multiple threads, or inside some seemingly random callback. Allocations, open files, and initialized variables are hard to clean up, and it is difficult to define which code should execute next after you enter an error condition. Setting up a try..catch block is difficult at best, and sometimes impossible. The result can be a mass of spaghetti code that is difficult for the original developer to understand, and nearly incomprehensible to others who are assigned the unfortunate task of maintaining it.
All of these problems are commonly encountered by developers who create asynchronous code. What Jeff has done is write a library that allows you to write asynchronous code in a synchronous style, as if each operation were occurring in a linear, or serial, sequence. In other words, you can write a single method in which the file is first downloaded, then parsed, and data is then inserted in a database. The code looks like synchronous code, and appears to execute in a linear fashion. Behind the scenes, however, the code is actually asynchronous, and uses multiple threads. As mentioned earlier, the library is built around C# Iterators, which bear the weight of handling the multiple threads that are spawned during your asynchronous operations.
Take a look at the movie to learn exactly how it works, and then download Jeff's free library to try it yourself. Jeff is, of course, a great speaker, and his explanation of this library is a joy to hear. Not only does he show a simple way to write asynchronous code, but he also does a great job of explaining exactly how C# Iterators are put together. It's one of the best explanations of that important subject I've ever heard.
- Download or view the video: https://channel9.msdn.com/posts/Charles/Jeffrey-Richter-and-his-AsyncEnumerator/
- Visit Jeff's Web Site and download the library: https://wintellect.com/PowerThreading.aspx
Comments
Anonymous
December 03, 2008
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
December 04, 2008
The comment has been removedAnonymous
December 04, 2008
Wow - and there's even a Silverlight version! I'm sure if anyone has to write a data intensive Silverlight page which calls several web services they will find this very useful.Anonymous
December 05, 2008
This articles spends most of it's time talking about a problem no one wants to hear- how hard it is to write Asynchronous programs. We want to hear how to make it easy! The part we want to hear takes place in the last few paragraphs. This is what you get when you do not use your College English 101 skills in the real world.Anonymous
December 05, 2008
The comment has been removedAnonymous
December 06, 2008
I found the write-up most informative. I am farely new to C# (less than five months). I am an experienced VB and Java programmer, with some C experience, also. I prefer to know what the problem is first, then be told about a possible solution. Unless you know what the question is, the answer is meaningless. I am in the process of developing a program that has grown to require an asynchronous message queue. So this info is of great interest. Thank you.Anonymous
December 07, 2008
The comment has been removedAnonymous
December 09, 2008
"The man is teaching you to fish instead of giving you a fish." Excellent. I'll use that one.Anonymous
December 09, 2008
Jeff Richter Video on Asynchronous Programming and his Power Threading LibraryAnonymous
December 11, 2008
This looks similar to the Concurrency and Coordination Runtime (CCR).Anonymous
December 11, 2008
Come on, as sson as anyone reads about the 'yield' keyword, they're going to do essentially the same thing. void func() { callback = this.func; create_thread(); yeild; create_thread2(); yeild; .... } I agree it's elegant, but it's also obviousAnonymous
December 15, 2008
The comment has been removedAnonymous
December 15, 2008
I recently watched this video on the PowerThreading library: http://blogs.msdn.com/charlie/archive/2008Anonymous
January 06, 2009
I can see how this works very well for long running tasks, especially in a windows application. However, I'm currently working on a consumer/produce asynch program, and am wondering if you think this model is appropriate for that.Anonymous
January 29, 2009
How is this different than the CCR?Anonymous
January 29, 2009
How is this different than the CCR?Anonymous
February 05, 2009
Very interesting. However, I do not understand whether this is going to be incorporated into official Visual Studio 2010. I am currently working with Thread instances, ThreadPool and the BackgroundWorker. Two weeks ago I bought the pre-release e-book "C# 2008 and 2005 Threaded Programming: Beginner’s Guide", by Gaston C. Hillar, Packt Publishing
- http://www.packtpub.com/beginners-guide-for-C-sharp-2008-and-2005-threaded-programming/book The book includes a small framework for concurrent programming and helps in developing applications to exploit dynamic cores (multicore CPUs with different number of cores). Very useful and easy to understand. However, I am still working with every new threading library, to compare results.
Anonymous
February 05, 2009
I forgot... This free article can be interesting for those researching about threading options: http://www.packtpub.com/article/simplifying-parallelism-complexity-c-sharp It is part of Hillar's book or e-book Chapter 8.Anonymous
April 09, 2009
Simple synchronization with Iterators in C#Anonymous
August 15, 2009
As a student of both C# and the CLR, I now see how the power of the iterators occurs at the compiler level to simplify the Asynchronous Programming Model. Another work of art by Jeffrey Richter.