Sunday, June 11, 2006

.NET multithreading

FeedJournal, like any RSS aggregator, needs to be efficient when it is updating the list of subscribed feeds. It is obvious that a sequential polling of feeds (check each feed and proceed to the next after finishing with the previous) will be sub-optimal in terms of performance and user experience. The internet requests will need to occur in parallel for optimal performance. However, if your feed subscription list contains more than a trivial amount of feeds, you don't want to congest your Internet line with all of these request at the same time.

IE7's RSS infrastructure calls this throttling, and it limits the number of concurrent web requests to 4. I don't see a reason to differ from this approach and implemented the same system.

One of the great things about .NET 2.0 is the easy-to-use infrastructure for multithreading and thread synchronization. By just dragging the BackgroundWorker component to the main form of my application I have all the threading support I will ever need. Starting a new thread in the application's process is then as easy as calling RunWorkerAsync() on the BackgroundWorker object. This will queue a new thread to be executed as soon as there is an available slot.

In FeedJournal's case the scenario becomes a little bit more complicated since if I let the method that iterates the feed updates run in the same thread as the GUI, it will become unresponsive. Therefore the main thread calls RunWorkerAsync(). The background worker iterates over all feeds and calls ThreadPool.QueueUserWorkItem for each item.

Throttling the maximum number of concurrent downloads is then simply achieved by the line:

ThreadPool.SetMaxThreads(4, 1000);

telling the ThreadPool to have a maximum of 4 actively executing threads at any given time.

No comments:

Post a Comment