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
}
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.
You'll have to use reflection to get the method:
var method = this.GetType().GetMethod(myFunction, new Type[] { typeof(object) });
var d = (WaitCallback)Delegate.CreateDelegate(typeof(WaitCallback), this, method);
ThreadPool.QueueUserWorkItem(d);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.