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.

 

Jeff2

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.

 

kick it on DotNetKicks.com

Comments (20)

  1. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  2. Pop Catalin says:

    This is amazing, pure stroke of genius… seems to be the best asynchronous programming model discovered so far for .Net

  3. JasonBSteele says:

    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.

  4. Green Williams says:

    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.

  5. ccalvert says:

    Sorry Green, if you felt I was beating around the bush. I tried to hit the main points in the first two sentences, pointing to the video and describing why it is valuable. But from your comments, it sounds like I did not make things as clear as I could have. The lesson is probably that one always needs to take one’s time. I wrote this post at high speed, in just a few minutes, because I was rushed. When I do that kind of thing, I think I’m going to get away with it, but most of the time the posts that people really like are the ones that I spend more time fussing over….

  6. 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.

  7. Peter Wone says:

    Personally I’ve never found APM even remotely difficult. Define an execution context class and use it to store all shared context for each logical process. If you pass it around in ar.AyncState then everything will be in scope when you have to clean up.

    I don’t see eye to eye with Jeff Richter on any number of issues but his writing style is always appropriate. "We don’t want to understand all that crap, just give us the answers" is infantile. The man is teaching you to fish instead of giving you a fish.

  8. lukepuplett says:

    "The man is teaching you to fish instead of giving you a fish."

    Excellent. I’ll use that one.

  9. Jeff Richter Video on Asynchronous Programming and his Power Threading Library

  10. sellison says:

    This looks similar to the Concurrency and Coordination Runtime (CCR).

  11. Smitty says:

    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 obvious

  12. Jeffrey helped me so much to learn this stuff in a short time at the Dev converence a few months back in Wash.  Then he really was available via email like he said he would be.

    All hiost answers have panned out pretty much to the letter.

    I am a stupid guy so I love learning what is obvious to others.  My mind might not take that track.  So this really helped me again and I love it when I am tachable and open (I am a smartass though much of the time).

    thanks again Jeffery

  13. Paul MM says:

    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.

  14. Matt Valerio says:

    How is this different than the CCR?

  15. Matt Valerio says:

    How is this different than the CCR?

  16. Nicholas Carper says:

    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.

  17. Nicholas Carper says:

    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.

  18. Simple synchronization with Iterators in C#

  19. David Richter says:

    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.

Skip to main content