gpt4 book ai didi

c# - Dispatcher 在 Messagebox.Show in Textchanged 事件中抛出 InvalidOperationException

转载 作者:可可西里 更新时间:2023-11-01 08:19:20 25 4
gpt4 key购买 nike

首先这是关于我的错误的错误日志条目

崩溃程序 @ 15-9-2011 15:01:30error:System.InvalidOperationException: Dispatcher 处理已暂停,但仍在处理消息。
在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handled)

无论如何代码:

private void TB_postcode_cijfers_TextChanged(object sender, TextChangedEventArgs e){
if (TB_postcode_cijfers.Text != string.Empty || TB_postcode_cijfers.Text.Length > 0)
{
LBL_postcode.Content = Postcode_cijfers + Postcode_letters;
if (TB_postcode_cijfers.Text.Length == 4 && TB_postcode_letters.Text.Length == 2)
{
if (!ZoekOpPostcode(Injectioncheck(TB_postcode_cijfers.Text + TB_postcode_letters.Text)))
{
//MessageBox.Show("Geen resultaat gevonden, " + errortext);
if (MessageBox.Show("Geen resultaat gevonden, " + errortext + ".\n Wilt u overschakelen naar handmatig? ", "Handmatig?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
RB_handmatig.IsChecked = true;
}
else
{
//
}
}
}
}}

所以在 messagebox.show 方法上。只有当用户在我的表单上将阅读模式切换到编辑模式时才会发生这种情况。这涉及折叠并显示一些标签和 UI 控件。

如果事件从用户输入触发,一切都很好。我想知道什么:为什么在隐藏和显示一些控件时会触发 textchanged 事件。我能做些什么来防止这个错误?

编辑:上面的代码在自定义 wpf 控件中。放置在 winforms 项目/表单中

最佳答案

查看此 thread它描述了与您相同的问题:

The exception is done on purpose to prevent reentrancy bugs caused by weirdness resulting from altering the visual tree, while such an event (which itself has been triggered by the visual tree altering) is firing. If you really must confirm something when the state of a UI element changes, delaying with Dispatcher.BeginInvoke is probably the right thing to do.

要在 UI 线程上运行代码,请执行以下操作:

 Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{

if (MessageBox.Show("Geen resultaat gevonden, " + errortext + ".\n Wilt u overschakelen naar handmatig? ", "Handmatig?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
RB_handmatig.IsChecked = true;
}
else
{
//
}
}));

关于c# - Dispatcher 在 Messagebox.Show in Textchanged 事件中抛出 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442943/

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