调用线程必须为 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

posted @ 2011-04-15 16:30  山河  阅读(1437)  评论(0编辑  收藏  举报