調用線程必須爲 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();
     即可。

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/sshhbb/archive/2011/01/20/6155246.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章