Johan's Blog

This and that in a developer's life in general
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

ThreadPool.QueueUserWorkItem with Anonymous Types

I thought this blog post by Matt Valerio was good, and it gave me a few ideas to use in a current test project. He wrote a helper method to be able to use anonymous types when calling a delegate or lambda in the ThreadPool:

public delegate void WaitCallback<T>(T state);

public static class ThreadPoolHelper
{
    public static bool QueueUserWorkItem<T>(T state, WaitCallback<T> callback)
    {
        return ThreadPool.QueueUserWorkItem(s => callback((T)s), state);
    }
}

And his example code for using this method:

ThreadPoolHelper.QueueUserWorkItem(
    new { Name = "Matt", Age = 26 },
    (data) =>
    {
        string name = data.Name;
        int age = data.Age;
        // Long-running computation
    });

 

Cute, eh?


Cross-posted from my blog at http://weblogs.asp.net/jdanforth
Published den 3 juli 2008 11:10 by johan
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

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems