This blog has been moved to Redwerb.com.

Friday, November 10, 2006

The Holy Grail of .Net Threading

The Epiphany

I was working on a project that needed to be able to process a WebRequest/WebResponse from a thread within a WinForm application. I tried several approaches, including using a BackgroundWorker, however, what I really wanted to do was create my own component similar to the BackgroundWorker, but specifically for WebRequests. Unfortunately, I didn't know how they did it.

As I was driving home from work I had an epiphany. The obvious solution would be to use Reflector for .NET to figure out how Microsoft did it.

Meeting Up With Sir Galahad

I opened the class up in Reflector and used the File Disassembler (an add-in to Reflector) to output it as a VB class. I waded through the code trying to understand what it did when I found the first hint as to the location of the Holy Grail, the AsyncOperation class.

This class looked important in the threading for BackgroundWorker, so I looked up the documentation for it. In the first sentence of the remarks, it contained a link to the Holy Grail.

The Holy Grail of .Net Threading

The Event-based Asynchronous Pattern is a design pattern that is useful for creating a class that can run operations on a separate thread, but will raise events on the main thread (very useful for threading in WinForms).

MSDN has a series of articles that clearly describe this pattern and how to implement it. The Asynchronous Programming Design Patterns node in the MSDN documentation discusses several different asynchronous patterns that you can use.

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.