【问题标题】:.NET Cannot perform this operation while dispatcher processing is suspended.NET 无法在调度程序处理暂停时执行此操作
【发布时间】:2014-02-06 23:39:09
【问题描述】:

我在实现 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,Culture=neutral,PublicKeyToken=b77a5c561934e089 消息:调度程序处理暂停时无法执行此操作。 来源:WindowsBase 帮助链接: 数据:System.Collections.ListDictionaryInternal 目标站点:无效 PushFrame(System.Windows.Threading.DispatcherFrame) 堆栈跟踪:在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架) 在 System.Windows.Threading.DispatcherOperation.Wait(时间跨度超时) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象参数,布尔 isSingleParameter) 在 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 优先级,委托方法) 在 OCC600.Infrastructure.Dictionary.BusinessEntities.Observable.RaisePropertyChangedEventSynchronous(String propertyName)

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

我该如何解决这个问题?

【问题讨论】:

    标签: .net wpf


    【解决方案1】:

    我假设您在后台线程上并试图在 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)); 
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢,谢谢。这应该很简单。但是,要刷新集合,我必须在 CollectionView.Refresh() 方法上执行 dispatcher.invoke,否则会引发异常。
    • 为此,您应该使用 Dispatcher.BeginInvoke。没有其他解决方案,因为您需要在调度程序线程上,并且没有可靠的方法来判断调度程序何时被挂起。
    • 谢谢。再次检查,我正在使用 BeginInvoke 刷新集合。
    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 2016-03-21
    • 2010-11-15
    • 2014-06-20
    • 2018-03-26
    • 2016-05-12
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多