Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ThreadPool.QueueUserWorkItem中将字符串变量转换为WaitCallback_C#_Threadpool - Fatal编程技术网

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);