I was creating an application wich send data over the network from client to server.
(An application for calling somebody over a network , like a bell on a housedoor).
I created a thread wich connects the server and client , i wanted to refresh a label in the statusbar of the server-side application wich displayed the operation that was working.
In c# the most common thing you can do is this :
1. Create a delegate for your function you want to use in the invoke thread.private delegate void UpdateLogCallback(string strMessage);
2. Update the form, you can use this.invokethis.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { “Connected Successfully!” });
3. the function wich is called from the connection-thread :private void UpdateLog(string strMessage)
{
lblsts.Text(strMessage);
}
In WPF you can’t use the invoke method after this.
To work arround this you can use the the Invoke methods on the associated dispatcher :lblSts.Dispatcher.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { “Connected Successfully!” });
本文介绍如何在WPF和Winforms应用程序中实现跨线程更新UI元素的方法。对于WPF,可以通过Dispatcher.Invoke方法进行调用;而对于Winforms,则可以使用Invoke方法来完成这一操作。
1万+

被折叠的 条评论
为什么被折叠?



