WPF线程之(二)——Dispatcher详解2

当我们打开一个WPF应用程序即开启了一个进程,该进程中至少包含两个线程。

  • 一个线程用于处理呈现:隐藏在后台运行
  • 一个线程用于管理用户界面:接收输入、处理事件、绘制屏幕以及运行应用程序代码。即UI线程。

在UI线程中有一个Dispatcher对象,管理每一个需要执行的工作项。Dispatcher会根据每个工作项的优先级排队。向Dispatcher列队中添加工作项时可指定10个不同的级别。那么问题来了,如果遇到耗时操作的时候,该操作如果依旧发生在UI线程中,Dispatcher 列队中其他的需要执行的工作项都要等待,从而造成界面假死的现象。为了加快响应速度,提高用户体验,我们应该尽量保证Dispatcher 列队中工作项要。所以,对于耗时操作,我们应该开辟一个新的子线程去处理,在操作完成后,通过向UI线程的Dispatcher列队注册工作项,来通知UI线程更新结果。

Dispatcher提供两个注册工作项的方法:

Invoke 和 BeginInvoke。这两个方法均调度一个委托来执行。

Invoke 是同步调用,也就是说,直到 UI 线程实际执行完该委托它才返回。

BeginInvoke是异步的,将立即返回。

  • Dispatcher实际上并不是多线程
  • 子线程不能直接修改UI线程,必须通过向UI线程中的Dispatcher注册工作项来完成
  • Dispatcher 是单例模式,暴露了一个静态的CurrentDispatcher方法用于获得当前线程的Dispatcher
  • 每一个UI线程都至少有一个Dispatcher,一个Dispatcher只能在一个线程中执行工作。
  • 开启新线程的方法很多,比如delegate.BeginInvoke()的方式开启的新线程。

Delegate.Invoke: Executes synchronously, on the same thread.
Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.

示例程序

XAML

<Window x:Class="DispatcherExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="160" Width="300">
    <StackPanel>
        <ProgressBar Name="progressBar" Height="20" Width="250" Margin="10"></ProgressBar>
        <TextBox Name="textBox" Width="50" Height="20" HorizontalAlignment="Center"></TextBox>
        <Button Name="btnProcess" Width="100" Click="btnProcess_Click" Margin="5">Start</Button>
        <Button Name="btnCancel" Width="100" Click="btnCancel_Click" Margin="5">Cancel</Button>
    </StackPanel>
</Window>

C#

namespace DispatcherExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Thread taskThread;
        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            taskThread = new Thread(DoTask);
            taskThread.Start();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            taskThread.Abort();
            MessageBox.Show("Background task finished normally", "info");
            this.progressBar.Value = 0;
            this.textBox.Text = null;
        }

        private void DoTask()
        {
            Int64 InputNum = (Int64)100;
            for (Int64 i = 0; i < InputNum; i++)
            {
                Thread.Sleep(100);
                this.Dispatcher.BeginInvoke((Action)delegate()
                {
                    this.progressBar.Value = i;
                    this.textBox.Text = i.ToString();
                });
            }
            MessageBox.Show("Background task has been canceled", "info");
            this.Dispatcher.BeginInvoke((Action)delegate()
                {
                    this.progressBar.Value = 0;
                    this.textBox.Text = null;
                });
        }
    }
}

演示




猜你喜欢

转载自blog.csdn.net/yinweimumu/article/details/80580421