调度员抛出InvalidOperationException异常的Messagebox.Show在TextChanged事件 [英] Dispatcher throws InvalidOperationException on Messagebox.Show in Textchanged event

查看:390
本文介绍了调度员抛出InvalidOperationException异常的Messagebox.Show在TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一切首先是我的错误错误日志条目

First of all this is the errorlog entry on my error

应急计划@ 15-9-2011 15:01:30error信息:System.InvalidOperationException:调度处理已经被停职,但消息仍在处理中。
   在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr的HWND,味精的Int32,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)

反正code:

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控件。

So on the messagebox.show method. this only happens when the user switches read mode to edit mode on my form. this involves collapsing en showing some labels and ui controls.

如果从userinput一切事件触发的罚款。
我whant知道的:
为什么要隐藏和显示一些控件什么时候TextChanged事件火灾。
我能做些什么,以prevent这个错误?

if the event fires from userinput everything is fine. What I whant to know: Why does the textchanged event fire when hiding and showing a few controls. What can i do to prevent this error?

编辑:
上面的code是在自定义WPF控件。放置在一个WinForms项目/表单

the code above is in a custom wpf control. placed in a winforms project/form

推荐答案

请参阅此<一个href=\"http://social.msdn.microsoft.com/forums/en-US/wpf/thread/44962927-006e-4629-9aa3-100357861442\">thread它描述了同样的问题你的:

See this thread it describes the same problem as yours:

该例外被做的目的而引起的prevent重入的错误
  从古怪改变可视树造成,而这样的事件
  (它本身已经被可视树变更触发)是
  射击。如果你真的必须确认的东西时的状态
  UI元素变化,延缓 Dispatcher.BeginInvoke 可能是
  正确的事情。

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线程code执行以下操作:

To run code on the UI Thread do the following:

 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
        {
            //
        }
    }));

这篇关于调度员抛出InvalidOperationException异常的Messagebox.Show在TextChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆