.NET 在调度程序处理挂起时无法执行此操作

标签 .net wpf

我在实现 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);
    }
}

有时,我会将以下异常记录到文件中:

类型:System.InvalidOperationException、mscorlib、版本=2.0.0.0、文化=中性、PublicKeyToken=b77a5c561934e089 消息:调度程序处理暂停时无法执行此操作。 来源:WindowsBase 帮助链接: 数据:System.Collections.ListDictionaryInternal TargetSite:无效PushFrame(System.Windows.Threading.DispatcherFrame) 堆栈跟踪:位于 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架) 在 System.Windows.Threading.DispatcherOperation.Wait(TimeSpan 超时) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托(delegate)方法、对象参数、 bool 值 isSingleParameter) 在System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority优先级,委托(delegate)方法) 在 OCC600.Infrastruct.Dictionary.BusinessEntities.Observable.RaisePropertyChangedEventSynchronous(字符串 propertyName)

如果我使用 Dispatcher.BeginInvoke 更新 UI,我不会收到这些异常。但我发现使用 BeginInvoke 执行更新并不可靠,因为有时这些更改不会反射(reflect)在用户界面上。

如何解决这个问题?

最佳答案

我假设您位于后台线程上,并尝试在 UI 线程上引发 PropertyChanged 事件。我认为 WPF 会为您处理线程更改;你不应该这样做。

这是我编写的一些代码。 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>

C#:

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)); 
      }
    }
  }
}

关于.NET 在调度程序处理挂起时无法执行此操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4403464/

相关文章:

c# - WPF 动画 FPS 与 CPU 使用率 - 我是否期望太多?

WPF 数据网格样式

c# - 避免 UserControl 捕获鼠标滚轮滚动

c# - Automapper 成员的自定义配置仅支持类型上的顶级个人成员

.NET 标准与 .NET 核心

.net - 创建 XML 并通过 HTTP post 发送

c# - 带有 PART_Editor 作为 Itemssource 项的 WPF ComboBox

c# - 在 C# 应用程序中处理位图

c# - XDocument - 表达式帮助

c# - WPF/MVVM :'Could not load file or assembly' 和 'Type reference cannot find type'