CoNatural.Threading.dll?

Roger Torres - February 5th, 2009

Three weeks ago I started working on an email archiving system based on Microsoft Exchange Server 2003. After researching a bit about all the possible ways to interface with Exchange, I decided to try the .NET System.DirectoryServices component to query Active Directory (via LDAP) for users/mailboxes, and complement with System.Net web requests (via WebDAV) to manage exchange items (folders, mail, contacts, etc).

After a few hours struggling with the .NET asynchronous programming model, I started “googling” again for options. The problem was that I was trying to invoke multiple asynchronous operations in parallel to achieve maximum performance, but the resulting code was getting too disorganized and hard to follow. All I needed was to find a way to continue experiencing the natural sequential feeling of my imperative code without sacrificing parallelism. The ideal solution would start background threads from the thread pool automatically when required, dealing with GUI synchronization context issues as well.

I found a group of very interesting articles, all showing various ways to solve my problem by leveraging the new functional language features of C# 3.0. With a combination of lambda expressions, closures, and a hack to simulate (more or less) coroutines via C# generators (or iterators), I was able to see the light at the end of the tunnel.

Here are some links to the best articles I found:

http://msdn.microsoft.com/en-us/magazine/cc163323.aspx

http://blogs.msdn.com/michen/archive/2006/03/30/564671.aspx

http://tomasp.net/blog/csharp-async.aspx

http://msmvps.com/blogs/mihailik/archive/2005/12/26/79813.aspx

http://tirania.org/blog/archive/2003/Apr-22.html

After reading all of them (and sorting many of these brilliant ideas out), I felt compelled to write a tool that was more accessible to .NET developers, a tool that will eventually become handy to complete my mail archiving application.

Last night I finally settled for a pattern that seemed well balanced in terms of complexity and functionality. I still need to implement progress reporting and completion/cancellation events though, but I will start this series describing my line of thinking in parallel with the final coding touches ;-)

I will name and encapsulate this component inside CoNatural.Threading.dll for a change.

Stay tuned.

Leave a comment