.NET Cannot complete this operation while dispatcher processing is paused

I have the following method in a base class that implements System.ComponentModel.INotifyPropertyChanged:

protected virtual void RaisePropertyChangedEventSynchronous(string propertyName) { try { if (PropertyChanged != null) { Delegate[] delegates = this.PropertyChanged.GetInvocationList(); foreach (PropertyChangedEventHandler handler in delegates) { try { DispatcherObject dispatcherInvoker = handler.Target as DispatcherObject; if (dispatcherInvoker != null) { dispatcherInvoker.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { handler(this, new PropertyChangedEventArgs(propertyName)); })); } else { handler(this, new PropertyChangedEventArgs(propertyName)); } } catch (Exception ex) { ExceptionPolicy.HandleException(ex, ExceptionHandlingPolicyNames.LogPolicy); } } } } catch (Exception ex) { ExceptionPolicy.HandleException(ex, ExceptionHandlingPolicyNames.LogPolicy); } } 

Sometimes I get the following exception registered in the file:

Type: System.InvalidOperationException, mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 Message: This operation cannot be completed while dispatcher processing is paused. Source: WindowsBase Reference link: Data: System.Collections.ListDictionaryInternal TargetSite: Void PushFrame (System.Windows.Threading.DispatcherFrame) Stack trace: in System.Windows.Threading.Dispatcher.PushFrame (DispatcherFrame) in System.Windows.Threading .DispatcherOperation.Wait (TimeSpan timeout) in System.Windows.Threading.Dispatcher.InvokeImpl (DispatcherPriority priority, TimeSpan timeout, delegation method, object arguments, logical isSingleParameter) in System.Windows.Threading.Dispatcher.Invoke (priority DispatcherPriority, Delegate method) in OCC600.Infrastructure.Dictionary.BusinessEntities.Observable.RaisePropertyChangedEventSynchronous (String propertyName)

If I use Dispatcher.BeginInvoke to update the user interface, I do not get these exceptions. But I found that performing updates using BeginInvoke is not reliable, because sometimes these changes do not affect the user interface.

How can I solve this problem?

+8
wpf
source share
1 answer

I assume that you are in the background thread and are trying to raise your PropertyChanged event in the user interface thread. I think WPF handles thread change for you; you do not need to do this.

Here is the code I wrote. XAML:

 <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBlock Text="{Binding Value}" /> </Grid> 

FROM#:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new Backgrounder(); } class Backgrounder : INotifyPropertyChanged { int value = 0; public Backgrounder() { ThreadPool.QueueUserWorkItem(o => { while (true) { this.value++; Notify("Value"); Thread.Sleep(TimeSpan.FromSeconds(1)); } }); } public int Value { get { return this.value; } } public event PropertyChangedEventHandler PropertyChanged; void Notify(string property) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(property)); } } } } 
+8
source share

Source: https://habr.com/ru/post/650002/


All Articles