本章讲述:WPF Dispatcher.Invoke和Dispatcher.BeginInvoke的区别。
该类型的 CollectionView 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改。
Task任务中或者线程中可用App.Current.Dispatcher.Invoke(() =>{});(等待主线程调用)更新界面上,不允许线程调度的显示或者操作 ;
在 WPF中,只有创建 DispatcherObject 的线程才能访问该对象。 例如,从主 UI 线程旋转的后台线程无法更新在 UI 线程上创建的 Button 的内容。 为了让后台线程访问 Button的 Content 属性,后台线程必须将工作委托给与 UI 线程关联的Dispatcher。 这是通过使用 Invoke 或 BeginInvoke来实现的。
Invoke 同步,BeginInvoke 是异步的。 操作将添加到指定 DispatcherPriorityDispatcher 的事件队列。
Invoke 是一种同步操作;因此,在回调返回后,控件才会返回到调用对象。
System.Windows.Application.Current.Dispatcher.BeginInvoke((System.Action)(() =>{})); 异步调用
BeginInvoke 是异步的;因此,控件在调用后立即返回到调用对象。
下面以一个简单的示例进行说明:
界面UI布局实现:
<Window x:Class="ScreenDisplayChangeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="150" Width="225">
<Grid>
<Button x:Name="btnTest" Content="控件内容更新测试" Width="150" Height="40" Click="btnTest_Click"/>
</Grid>
</Window>
后台逻辑实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ScreenDisplayChangeTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
this.btnTest.Content = "主线程直接更新";
//线程中直接更新
Task.Run(() =>
{
this.btnTest.Content = "Task中更新";
});
//Invoke
Task.Run(() =>
{
App.Current.Dispatcher.Invoke(() =>
{
this.btnTest.Content = "使用Invoke 更新";
});
});
//BeginInvoke 更新
Task.Run(() =>
{
App.Current.Dispatcher.BeginInvoke((System.Action)(() =>
{
this.btnTest.Content = "使用BeginInvoke更新";
}));
});
}
}
}
按下“F5”调试代码,会报出如下图异常信息:“调用线程无法访问此对象,因为另一个线程拥有该对象”;诸如此问题,可采用异步更新或者Invoke更新。