Tuesday, December 13, 2005

Anonymous Method Trick

I found out a quick way of using anonymous methods to wrap a call:


WaitCallback callback = delegate(object client)
{
processString((string)client);
};

ThreadPool.QueueUserWorkItem (callback, "the string");


the target method:


void processString(string initstring)
{
...
}


Because the QueueUserWorkItem uses the delegate WaitCallback which accepts an "object" we can use the anon method to do the casting preserving the target method signature.