大家好 我是一種新的這一切多線程的東西,所以請原諒我,如果我來解釋一下這個厲害:)執行的方法/設置屬性
比方說,我有兩個類:
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