[英]WPF multithreading. Changing the interface of a third-party flow
WPF C#。 我有一個綁定到其他方法的方法。 服務器發送一個問候世界。
var clientobj =
(OperClass) Activator.GetObject
(
typeof (OperClass),
"tcp :/ / localhost: 100001/TcpClient"
);
clientobj.Update ("HELLO WORLD");
客戶應用程序:
public void Update (string msg)
{
label1.text = msg; / / error thread
}
該程序用於通信RemotingServices.Marshal。 如何將文本更改為label1。 調度員無濟於事。
您不能從不是創建UI元素的線程的線程訪問UI元素。 為了克服這個問題,您將需要在創建UI元素的Dispatcher
線程上調用所需的內容。
假設clientobj
本身就是一個UI元素(例如Window
或UserControl
),那么可以使用以下代碼:
public void Update (string msg)
{
// See if we need to re-invoke on the Dispatcher thread
if (!CheckAccess())
{
// Invoke on the Dispatcher thread
this.Dispatcher.BeginInvoke(new Action<string>(Update), msg);
// Exit from this method to prevent continued execution
return;
}
// We are now running on the Dispatcher thread, so we can access the UI element(s) directly
label1.Text = msg;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.