在新线程中调用方法(字符串中的方法名称)? [英] Invoke method in new thread (method name from string)?
本文介绍了在新线程中调用方法(字符串中的方法名称)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在Winforms C#应用程序的新线程上调用方法.但是我需要方法名称来自字符串.
Im trying to invoke a method on a new thread in a winforms c# app. But I need the method name to come from a string.
是否可以做类似的事情:
Is it possible to do something like:
public void newThread(string MethodName)
{
new Thread(new ThreadStart(MethodName)).Start();
}
我已经尝试过,但是似乎无法使它正常工作?
I've tried but cant seem to get this to work?
任何建议将不胜感激.
推荐答案
我假设您想从类本身内部调用方法.
I am assuming you want to call method from within class itself.
Type classType = this.GetType();
object obj = Activator.CreateInstance(classType)
object[] parameters = new object[] { _objval };
MethodInfo mi = classType.GetMethod("MyMethod");
ThreadStart threadMain = delegate () { mi.Invoke(this, parameters); };
new System.Threading.Thread(threadMain).Start();
如果没有用所需的类替换this
.
If not replace this
with class you need.
这篇关于在新线程中调用方法(字符串中的方法名称)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文