private void Parallel(object sender, RoutedEventArgs e) { Task.Run(() => ChangeColour(Brushes.Red)); } private void Standard(object sender, RoutedEventArgs e) { ChangeColour(Brushes.Green); }
//如果是在控件的UI线程上执行 则直接调用就行,不在同一个ui线程上就使用线程调度的方式 private void ChangeColour(Brush brush) { if (MyGrid.CheckAccess()) MyGrid.Background = brush; else MyGrid.Dispatcher.Invoke(() => { MyGrid.Background = brush; }); }