Convert string variable to WaitCallback in ThreadPool.QueueUserWorkItem

Is it possible to pass string variable as a WaitCallback parameter in ThreadPool.QueueUserWorkItem()

string myFunction="Go";
ThreadPool.QueueUserWorkItem(MyFunction);


public void Go(object obj)
{
       //Do Something
}
Jon Skeet
people
quotationmark

You'll need to use reflection.

For example:

WaitCallback callback = (WaitCallback) Delegate.CreateDelegate(
    typeof(WaitCallback), this, myFunction);
ThreadPool.QueueUserWorkItem(callback);

To use a method in a different class, change this to the target instance. If you want to call a static method, use the overload of CreateDelegate which takes a Type as the second parameter rather than an object.

people

See more on this question at Stackoverflow