WinForms
private delegate void UpdateUiTextDelegate(Control control, string text);
private void UpdateUiText(Control control, string text)
{
if(InvokeRequired)
{
Invoke(new UpdateUiTextDelegate(UpdateUiText), new object[] {control, text});
return;
}
control.Text = text;
}
WPF
private void UpdateUiText(Control control, string text)
{
if(!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(UpdateUiText), control, text);
return;
}
control.Text = text;
}
源贴网址:http://cskardon.wordpress.com/2008/01/03/invoking-ui-changes-in-wpf/
本文介绍了在WinForms和WPF中更新用户界面文本的两种方法。针对WinForms, 使用Invoke方法确保在主线程上更新UI;而在WPF环境中,则利用Dispatcher来实现跨线程更新UI的目的。这些方法有助于避免因尝试直接从非UI线程修改UI元素而引发的异常。
4940

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



