[同步]Invoke
Application.Current.Dispatcher.Invoke(AutoIncreaseNumber);
[异步]BeginInvoke
Application.Current.Dispatcher.BeginInvoke((Action)AutoIncreaseNumber);
两者都会阻塞UI线程
基于WPF4.5.1示例
Invoke 按钮对应的是InvokeCommand
BeginInvoke按钮对应的是BeginInvokeCommand
可以发现,在执行按钮的命令时,UI线程是会阻塞,计时器并不会走动


1 public class MainViewModel : ViewModelBase 2 { 3 public MainViewModel() 4 { 5 DispatcherTimer timer = new DispatcherTimer(); 6 timer.Interval = TimeSpan.FromSeconds(1); 7 timer.Tick += timer_Tick; 8 timer.Start(); 9 } 10 11 void timer_Tick(object sender, EventArgs e) 12 { 13 Now = DateTime.Now; 14 } 15 16 private DateTime now = DateTime.Now; 17 18 public DateTime Now 19 { 20 get { return now; } 21 set 22 { 23 now = value; 24 RaisePropertyChanged("Now"); 25 } 26 } 27 28 29 30 private int number; 31 /// <summary> 32 /// 数值用于显示 33 /// </summary>