调用线程必须为STA,由于许多UI组件都须要。

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

相关文章
相关标签/搜索