Welcome to MSDN Blogs Sign in | Join | Help

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
Published Wednesday, December 03, 2008 10:23 PM by Charlie Calvert
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Jeff Richter Video on Asynchronous Programming and his Power Threading

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, December 04, 2008 1:27 AM by DotNetKicks.com

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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

Thursday, December 04, 2008 11:29 AM by Pop Catalin

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Thursday, December 04, 2008 3:37 PM by JasonBSteele

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Friday, December 05, 2008 11:42 AM by Green Williams

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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

Friday, December 05, 2008 3:01 PM by Charlie Calvert

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Sunday, December 07, 2008 12:33 AM by Brad Willcott

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Sunday, December 07, 2008 11:30 PM by Peter Wone

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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

Excellent. I'll use that one.

Tuesday, December 09, 2008 8:45 AM by lukepuplett

# Jeff Richter Video on Asynchronous Programming and his Power Threading Library

Jeff Richter Video on Asynchronous Programming and his Power Threading Library

Wednesday, December 10, 2008 4:30 AM by Blog of Developer Mikkel Ovesen

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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

Thursday, December 11, 2008 11:46 AM by sellison

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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

Friday, December 12, 2008 4:53 AM by Smitty

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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

Monday, December 15, 2008 11:41 AM by Glenn Kessler

# Async Coding using IDisposable

I recently watched this video on the PowerThreading library: http://blogs.msdn.com/charlie/archive/2008

Monday, December 15, 2008 12:54 PM by What are we going to do tomorrow night, Brain?

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Tuesday, January 06, 2009 4:57 PM by Paul MM

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

How is this different than the CCR?

Thursday, January 29, 2009 1:13 PM by Matt Valerio

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

How is this different than the CCR?

Thursday, January 29, 2009 1:13 PM by Matt Valerio

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Thursday, February 05, 2009 6:02 PM by Nicholas Carper

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Thursday, February 05, 2009 6:04 PM by Nicholas Carper

# Simple synchronization with Iterators in C#

Simple synchronization with Iterators in C#

Thursday, April 09, 2009 5:31 PM by justnbusiness

# re: Jeff Richter Video on Asynchronous Programming and his Power Threading Library

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.

Sunday, August 16, 2009 2:24 AM by David Richter

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker