Thread NetServer = new Thread(new ThreadStart(NetServerThreadFunc));
NetServer.Start();
WPF工程里,此线程不可以操作UI元素,避免方法如下:
1、public delegate void DeleFunc();
public void Func()
{
//使用ui元素
}
线程函数中做如此调用:
System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new DeleFunc(Func));
即可。
2、 Thread NetServer = new Thread(new ThreadStart(NetServerThreadFunc));
NetServer .SetApartmentState(ApartmentState.STA);
NetServer .IsBackground = true;
NetServer.Start();
线程函数中做如此调用:
System.Windows.Threading.Dispatcher.Run();
即可。
本文介绍了在WPF应用程序中如何从非UI线程安全地更新UI元素的方法。提供了两种实用的技术:一是通过Dispatcher.Invoke机制;二是通过设置线程属性并运行Dispatcher。这两种方法能够帮助开发者解决线程间UI更新的问题。

5746

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



