原文鏈接:點擊打開鏈接
Thread NetServer = new Thread(new ThreadStart(NetServerThreadFunc));NetServer.Start();WPF工程里,此線程不可以操作UI元素,避免方法如下:
1、
public delegate void DeleFunc();public void Func(){ //使用ui元素}線程函數(shù)中做如此調(diào)用:
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();線程函數(shù)中做如此調(diào)用:
System.Windows.Threading.Dispatcher.Run();即可。
新聞熱點
疑難解答