[同步]Invoke

Application.Current.Dispatcher.Invoke(AutoIncreaseNumber);

[异步]BeginInvoke

Application.Current.Dispatcher.BeginInvoke((Action)AutoIncreaseNumber);

两者都会阻塞UI线程

基于WPF4.5.1示例

Dispatcher中Invoke与BeginInvoke

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>
 34         public int Number
 35         {
 36             get { return number; }
 37             set
 38             {
 39                 number = value;
 40                 RaisePropertyChanged("Number");
 41             }
 42         }
 43 
 44         private bool isIncrease;
 45         /// <summary>
 46         /// 是否可以自增长
 47         /// </summary>
 48         public bool IsIncrease
 49         {
 50             get { return isIncrease; }
 51             set
 52             {
 53                 isIncrease = value;
 54                 RaisePropertyChanged("IsIncrease");
 55             }
 56         }
 57 
 58         /// <summary>
 59         /// 自动增长
 60         /// </summary>
 61         private void AutoIncreaseNumber()
 62         {
 63             IsIncrease = !isIncrease;
 64             while (IsIncrease && Number < 500000)
 65             {
 66                 Number++;
 67             }
 68         }
 69 
 70         #region RelayCommands
 71         /// <summary>
 72         /// Invoke命令 
 73         /// </summary>
 74         public RelayCommand InvokeCommand
 75         {
 76             get
 77             {
 78                 return new RelayCommand(() =>
 79                 {
 80                     Application.Current.Dispatcher.Invoke(AutoIncreaseNumber);
 81                 });
 82             }
 83         }
 84         /// <summary>
 85         /// BeginInvoke命令
 86         /// </summary>
 87         public RelayCommand BeginInvokeCommand
 88         {
 89             get
 90             {
 91                 return new RelayCommand(() =>
 92                 {
 93                     //这里直接使用匿名方法会报错
 94                     //'Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type'
 95                     //使用强制转换的方式
 96                     Application.Current.Dispatcher.BeginInvoke((Action)AutoIncreaseNumber);
 97                 });
 98             }
 99         }
100         /// <summary>
101         /// 清理数字命令
102         /// </summary>
103         public RelayCommand ClearCommand
104         {
105             get
106             {
107                 return new RelayCommand(() =>
108                     {
109                         Number = 0;
110                         IsIncrease = false;
111                     });
112             }
113         }
114         #endregion
115     }
View Code

相关文章: