當ObservableCollection列表被UI線程佔用時,若是在異步線程中調用ObservableCollection,會彈出如下異常:框架
咱們使用一個viewModel,在ViewModel中添加ObservableCollection類型的ItemsSource列表。異步
在列表使用ListBox綁定ItemsSource列表。再由界面觸發對ItemsSource的修改。ide
1 public class ViewModel : INotifyPropertyChanged 2 { 3 private ObservableCollection<string> _itemsSource = new ObservableCollection<string>(); 4 5 public ObservableCollection<string> ItemsSource 6 { 7 get => _itemsSource; 8 set 9 { 10 _itemsSource = value; 11 OnPropertyChanged(); 12 } 13 } 14 15 public event PropertyChangedEventHandler PropertyChanged; 16 17 [NotifyPropertyChangedInvocator] 18 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 19 { 20 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 21 } 22 }
1. 直接在異步線程下修改ObservableCollection--報錯this
1 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 2 { 3 var viewModel = this.DataContext as ViewModel; 4 Task.Run(() => 5 { 6 //此段調用異常 7 viewModel.ItemsSource.Add("test1"); 8 }); 9 }
2. 在異步線程下,賦值ObservableCollection--正常spa
1 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 2 { 3 var viewModel = this.DataContext as ViewModel; 4 Task.Run(() => 5 { 6 //此段不會報錯 7 var list = viewModel.ItemsSource.ToList(); 8 list.Add("test0"); 9 viewModel.ItemsSource = new ObservableCollection<string>(list); 10 }); 11 }
3. 在異步線程下,賦值ObservableCollection後,再修改ObservableCollection--正常線程
1 private void Button1_OnClick(object sender, RoutedEventArgs e) 2 { 3 var viewModel = this.DataContext as ViewModel; 4 Task.Run(() => 5 { 6 //此段不會報錯 7 viewModel.ItemsSource = new ObservableCollection<string>(new List<string>() { "test3", "test2" }); 8 //此段不會報錯 9 viewModel.ItemsSource.Add("test4"); 10 }); 11 }
在異步線程下設置的ItemsSource,能夠被當前異步線程調用。code
4. 異步線程下賦值ObservableCollection,而後在UI線程修改ObservableCollection--正常blog
1 private void Button1_OnClick(object sender, RoutedEventArgs e) 2 { 3 var viewModel = this.DataContext as ViewModel; 4 Task.Run(() => 5 { 6 //此段不會報錯 7 viewModel.ItemsSource = new ObservableCollection<string>(new List<string>() { "test0" }); 8 }); 9 } 10 11 12 private void Button2_OnClick(object sender, RoutedEventArgs e) 13 { 14 var viewModel = this.DataContext as ViewModel; 15 //此段不會報錯 16 viewModel.ItemsSource.Add("test2"); 17 }
在異步線程下設置的ItemsSource,能夠被UI線程調用。此處能夠理解爲,賦值時,框架默默轉到UI線程處理了?get
可是上面3流程,爲什麼正常,so weird~string
5. 異步線程下,回到UI線程中,修改ObservableCollection--正常
1 private void Button1_OnClick(object sender, RoutedEventArgs e) 2 { 3 var viewModel = this.DataContext as ViewModel; 4 Task.Run(() => 5 { 6 Application.Current.Dispatcher.Invoke(() => 7 { 8 //此段不會報錯 9 viewModel.ItemsSource.Add("test"); 10 }); 11 }); 12 }