C# 在ThreadPool.QueueUserWorkItem中将字符串变量转换为WaitCallback
是否可以在C# 在ThreadPool.QueueUserWorkItem中将字符串变量转换为WaitCallback,c#,threadpool,C#,Threadpool,是否可以在ThreadPool.QueueUserWorkItem()中将字符串变量作为WaitCallback参数传递 你需要使用反射 例如: WaitCallback callback = (WaitCallback) Delegate.CreateDelegate( typeof(WaitCallback), this, myFunction); ThreadPool.QueueUserWorkItem(callback); 若要在其他类中使用方法,请将this更改为目标实例。如
ThreadPool.QueueUserWorkItem()中将字符串变量作为WaitCallback参数传递
你需要使用反射
例如:
WaitCallback callback = (WaitCallback) Delegate.CreateDelegate(
typeof(WaitCallback), this, myFunction);
ThreadPool.QueueUserWorkItem(callback);
若要在其他类中使用方法,请将this
更改为目标实例。如果要调用静态方法,请使用CreateDelegate
的重载,该重载将类型
作为第二个参数,而不是对象。您需要使用反射
例如:
WaitCallback callback = (WaitCallback) Delegate.CreateDelegate(
typeof(WaitCallback), this, myFunction);
ThreadPool.QueueUserWorkItem(callback);
若要在其他类中使用方法,请将this
更改为目标实例。如果要调用静态方法,请使用CreateDelegate
的重载,该重载将类型
作为第二个参数,而不是对象。必须使用反射来获取方法:
var method = this.GetType().GetMethod(myFunction, new Type[] { typeof(object) });
var d = (WaitCallback)Delegate.CreateDelegate(typeof(WaitCallback), this, method);
ThreadPool.QueueUserWorkItem(d);
您必须使用反射来获取方法:
var method = this.GetType().GetMethod(myFunction, new Type[] { typeof(object) });
var d = (WaitCallback)Delegate.CreateDelegate(typeof(WaitCallback), this, method);
ThreadPool.QueueUserWorkItem(d);