WPF跨線程操作UI界面控件


   在WPF應用中,如果遇到多線程的需求時,如果引用WPF控件時會引發異常,異常內容:調用線程無法訪問此對象,因為另一個線程擁有該對象。具體如下: 調用代碼: ThreadcountThread= new Thread( new ThreadStart(Count)); countThread.Start(); 在調用的Count方法引發如下

  在WPF應用中,如果遇到多線程的需求時,如果引用WPF控件時會引發異常,異常內容:調用線程無法訪問此對象,因為另一個線程擁有該對象。具體如下:

  調用代碼:

Thread countThread = 

new Thread(new ThreadStart(Count));
countThread.Start();

  在調用的Count方法引發如下異常

 

 

  WPF 對象是從 DispatcherObject 派生的,這提供了用於處理並發和線程的基本構造。 WPF 基於調度程序實現的消息系統。 其工作方式與常見的 Win32 消息泵非常類似;事實上,WPF 調度程序使用 User32 消息執行跨線程調用。當WPF用戶線程中更新UI時,要通過Dispatcher來進行。

  調用方式參見如下代碼:

namespace WpfThreadTest
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        Thread countThread;
       
        public MainWindow()
        {
            InitializeComponent();
            this.textBox1.Text = DateTime.Now.ToLocalTime().ToString("yyyy年MM月dd日 hh:mm:ss"); ;
            countThread = new Thread(new ThreadStart(DispatcherThread));
        }
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (button3.Content.ToString() == "開始時間線程")
            {
                button3.Content = "停止時間線程";
                if (countThread.ThreadState == ThreadState.Suspended)
                {
                    //線程繼續
                    countThread.Resume();
                }
                else
                    countThread.Start();
            }
            else
            {
                button3.Content = "開始時間線程";  
                //線程掛起
                countThread.Suspend();
                
            }
        }
        public void DispatcherThread()
        {
            //可以通過循環條件來控制UI的更新
            while (true)
            {
                ///線程優先級,最長超時時間,方法委托(無參方法)
                textBox1.Dispatcher.Invoke(
                    DispatcherPriority.Normal, TimeSpan.FromSeconds(1), new Action(UpdateTime));
                Thread.Sleep(1000);                
            }
        }

        
        private void UpdateTime()
        {
            this.textBox1.Text = DateTime.Now.ToLocalTime().ToString("yyyy年MM月dd日 hh:mm:ss");      
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            countThread.Abort();
            Application.Current.Shutdown();
        }
    }
}

 

  運行效果如圖

 

 

 


免責聲明!

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



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