gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:26:02 26 4
gpt4 key购买 nike

我在实现 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.ListDictionaryInternalTargetSite:无效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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com