大家好 我是一种新的这一切多线程的东西,所以请原谅我,如果我来解释一下这个厉害:)执行的方法/设置属性
比方说,我有两个类:
class abc {
public string SomeProperty {
get { return something; }
set { /* This code has to execute in the main application thread */ }
}
public void SomeMethod() {
/* This code also has to execute in the main application thread */
}
}
class def {
abc obj;
public def() {
abc = new obj();
}
public void SomeMethod() {
abc.SomeProperty = "SomeValue";
abc.SomeMethod();
}
}
我遇到的问题是获取SomeProperty和SomeMethod在主应用程序线程上执行。我曾尝试:
abc.GetType().InvokeMember("SomeProperty", System.Reflection.BindingFlags.SetProperty, null, abc, new object[] { "SomeValue" });
abc.GetType().InvokeMember("SomeMethod", System.Reflection.BindingFlags.InvokeMethod, null, abc, null);
但是,没有被主应用程序线程中执行它需要在主应用程序线程来执行代码,即使使用InvokeMember(我不这么认为反正) 我有尝试在代码中输出当前线程名称,并且不输出主应用程序线程名称。
有没有办法可以做到这一点? 如果我已经解释不好,只是让我知道:) 谢谢!
请解释为什么** **你需要执行在主要(或其他)线程上的代码。你使用'InvokeMember'的方法显然是错误的,没有明确你的意图,你不能指望一个真正有用的答案,将符合你的实际案例。 – 2011-05-03 16:08:47
并请指定WinForms或WPF – 2011-05-03 16:11:08
抱歉缺少信息。这不是WinForms或WPF,它是一个控制台应用程序。有很多原因需要在主线程上执行,其中之一是因为我正在构建一个使用Lidgren网络库的应用程序,并且我只能在该库正在监听的线程上发送网络消息。 – Jamie 2011-05-03 17:17:59