现在的位置: 首页 > 综合 > 正文

WPF中UI线程和工作线程的交流

2012年05月22日 ⁄ 综合 ⁄ 共 610字 ⁄ 字号 评论关闭

在WPF中,UI线程中的界面物体是不能被UI以外的线程修改的,如果你想在非UI线程中修改界面物体值的话,可以使用System.Windows.Threading.Dispatcher类中的Invoke方法

public void DoWork()
{
ThreadPool.QueueUserWorkItem((WaitCallback)
delegate(object o)  
            
{  //现在在非UI线程中
                bool isConnected = false;
                                               Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)
delegate()
                    
{
                        WriteMessageToWin(
"cannot connect to  server");
                    }
);

                }


 
private TextBlock WriteMessageToWin(string message)
        
{         
            tbTextBlock.Text 
= message;
}

抱歉!评论已关闭.