我在实现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,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 消息:暂停调度程序处理时无法执行此操作。 来源:WindowsBase 帮助链接: 数据:System.Collections.ListDictionaryInternal TargetSite:Void PushFrame(System.Windows.Threading.DispatcherFrame) 堆栈跟踪:在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架) 在System.Windows.Threading.DispatcherOperation.Wait(TimeSpan超时) 在System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter) 在System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority,Delegate方法) at OCC600.Infrastructure.Dictionary.BusinessEntities.Observable.RaisePropertyChangedEventSynchronous(String propertyName)
如果我使用Dispatcher.BeginInvoke更新UI,我不会得到这些例外。但我发现使用BeginInvoke执行更新并不可靠,因为有时这些更改不会反映在用户界面上。
我该如何解决这个问题?
答案 0 :(得分:8)
我认为你是在后台线程上,并且正在尝试在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));
}
}
}
}