日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

在WPF中使用多線程更新UI_C#教程

作者:天方 ? 更新時間: 2022-08-14 編程語言

有經(jīng)驗的程序員們都知道:不能在UI線程上進行耗時操作,那樣會造成界面卡頓,如下就是一個簡單的示例:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Dispatcher.Invoke(new Action(()=> { }));
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Content = new UserControl1();
        }
    }

    class UserControl1 : UserControl
    {
        TextBlock textBlock;

        public UserControl1()
        {
            textBlock = new TextBlock();
            this.Content = textBlock;

            this.Dispatcher.BeginInvoke(new Action(updateTime), null);
        }

        private async void updateTime()
        {
            while (true)
            {
                Thread.Sleep(900);            //模擬耗時操作

                textBlock.Text = DateTime.Now.ToString();
                await Task.Delay(100);
            }
        }
    }

當(dāng)我們運行這個程序的時候,就會發(fā)現(xiàn):由于主線程大部分的時間片被占用,無法及時處理系統(tǒng)事件(如鼠標(biāo),鍵盤等輸入),導(dǎo)致程序變得非常卡頓,連拖動窗口都變得不流暢;

如何解決這個問題呢,初學(xué)者可能想到的第一個方法就是新啟一個線程,在線程中執(zhí)行更新:

    public UserControl1()
    {
        textBlock = new TextBlock();
        this.Content = textBlock;

        ThreadPool.QueueUserWorkItem(_ => updateTime());
    }

但很快就會發(fā)現(xiàn)此路不通,因為WPF不允許跨線程訪問程序,此時我們會得到一個:"The calling thread cannot access this object because a different thread owns it."的InvalidOperationException異常

那么該如何解決這一問題呢?通常的做法是把耗時的函數(shù)放在線程池執(zhí)行,然后切回主線程更新UI顯示。前面的updateTime函數(shù)改寫如下:

    private async void updateTime()
    {
        while (true)
        {
            await Task.Run(() => Thread.Sleep(900));
            textBlock.Text = DateTime.Now.ToString();
            await Task.Delay(100);
        }
    }

這種方式能滿足我們的大部分需求。但是,有的操作是比較耗時間的。例如,在多窗口實時監(jiān)控的時候,我們就需要同時多十來個屏幕每秒鐘各進行幾十次的刷新,更新圖像這個操作必須在UI線程上進行,并且它有非常耗時間,此時又會回到最開始的卡頓的情況。

看起來這個問題無法解決,實際上,WPF只是不允許跨線程訪問程序,并非不允許多線程更新界面。我們大可以對每個視頻監(jiān)控窗口單獨其一個獨立的線程,在那個線程中進行更新操作,此時就不會影響到主線程。MSDN上有篇文章介紹了詳細(xì)的操作:Multithreaded UI: HostVisual。用這種方式將原來的程序改寫如下:

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        HostVisual hostVisual = new HostVisual();

        UIElement content = new VisualHost(hostVisual);
        this.Content = content;

        Thread thread = new Thread(new ThreadStart(() =>
        {
            VisualTarget visualTarget = new VisualTarget(hostVisual);
            var control = new UserControl1();
            control.Arrange(new Rect(new Point(), content.RenderSize));
            visualTarget.RootVisual = control;

            System.Windows.Threading.Dispatcher.Run();

        }));

        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();
    }

    public class VisualHost : FrameworkElement
    {
        Visual child;

        public VisualHost(Visual child)
        {
            if (child == null)
                throw new ArgumentException("child");

            this.child = child;
            AddVisualChild(child);
        }

        protected override Visual GetVisualChild(int index)
        {
            return (index == 0) ? child : null;
        }

        protected override int VisualChildrenCount
        {
            get { return 1; }
        }
    }

這個里面用來了兩個新的類:HostVisual、VisualTarget。以及自己寫的一個VisualHost。MSDN上相關(guān)的解釋,也不算難理解,這里就不多介紹了。最后,再來重構(gòu)一下代碼,把在新線程中創(chuàng)建控件的方式改寫如下:

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        createChildInNewThread<UserControl1>(this);
    }

    void createChildInNewThread<T>(ContentControl container)
        where T : UIElement , new()
    {
        HostVisual hostVisual = new HostVisual();

        UIElement content = new VisualHost(hostVisual);
        container.Content = content;

        Thread thread = new Thread(new ThreadStart(() =>
        {
            VisualTarget visualTarget = new VisualTarget(hostVisual);

            var control = new T();
            control.Arrange(new Rect(new Point(), content.RenderSize));

            visualTarget.RootVisual = control;
            System.Windows.Threading.Dispatcher.Run();

        }));

        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();
    }

當(dāng)然,我這個函數(shù)多了一些不必要的的限制:容器必須是ContentControl,子元素必須是UIElement。可以根據(jù)實際需要進行相關(guān)修改。這里有一個完整的示例,也可以參考一下。

原文鏈接:https://www.cnblogs.com/TianFang/p/3969430.html

欄目分類
最近更新