Welcome to WindowsClient.net | Sign in | Join

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)

Asynchronous Invocation - Enumerable Extension

I encountered several times in the following scenario:

I have a list of business objects.
My wish is to invoke an action on each of these objects, all of theses actions will be invoked asynchronously using the Thread Pool and it will block until all the asynchronous actions are complete.

I've written a useful extension that makes this scenario very simple to code.

var list = new[] { 1, 2, 3, 4, 5 };
list.WaitAsyncForEach(i => Console.WriteLine("Invoked async for '{0}'", i));

Note - with large enumerables you might consider doing it for a certain batch size at a time, or at least modify the code to enforce a size limitation of some kind.
This is due to the fact that "WaitHandle.WaitAll(waitHandles);" might throw a "NotSupportedException" if the number of objects in waitHandles is greater than the system permits.

A project is attached with the code and the example on how to use it. (Rename to zip)

In order to view the attached files, make sure you enter the specifc post page.

Posted: Feb 17 2008, 09:40 AM by zuker | with no comments
Filed under:

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter