【发布时间】: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 执行更新并不可靠,因为有时这些更改不会反映在用户界面上。
我该如何解决这个问题?
【问题讨论】: