調用線程必須為 STA,因為許多 UI 組件都需要


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();
即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM