Thread NetServer = new Thread(new ThreadStart(NetServerThreadFunc));
NetServer.Start();ide
WPF工程裏,此線程不能夠操做UI元素,若是在線程中調用了UI處理,則會彈出如上錯誤。避免方法以下:函數
一、public delegate void DeleFunc();
public void Func()
{ui
//使用ui元素 spa
}線程
線程函數中作如此調用:orm
System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,new DeleFunc(Func));it
便可。io
二、 Thread NetServer = new Thread(new ThreadStart(NetServerThreadFunc));
NetServer .SetApartmentState(ApartmentState.STA);
NetServer .IsBackground = true;class
NetServer.Start();
方法
線程函數中作如此調用:
System.Windows.Threading.Dispatcher.Run(); 便可。