12

First of all this is the errorlog entry on my error

crash program @ 15-9-2011 15:01:30error:System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed. at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

Anyway 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
            {
                //
            }
        }
    }
}}

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.

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?

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

Daanvl
  • 608
  • 1
  • 9
  • 24

1 Answers1

23

See this thread it describes the same problem as yours:

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.

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
        {
            //
        }
    }));
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • Dont know much about lambda's but this gives me Error Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type – Daanvl Sep 16 '11 at 11:28
  • That builds now, but gives NullReferenceException on the first line. I'm starting to suspect the dispatcher is not alive at that point? – Daanvl Sep 16 '11 at 11:57
  • Oke debugging tells me that Application.Current = null How is that even possible.. – Daanvl Sep 16 '11 at 12:03
  • 1
    I don't know why this would happen, is this a WPF project? otherwise you could use the dispatcher that is attached to each control, eg RB_handmatig.Dispatcher.BeginInvoke(). The call syntax is slightly different, but I'm sure you find examples about it – thumbmunkeys Sep 16 '11 at 12:16
  • Its actually a winforms project. But because where switching to wpf I got this control (wpf) placed on a winforms form. I'll put that in the question – Daanvl Sep 16 '11 at 12:21
  • I changed Application.current.dispatcher to > this.dispatcher and it worked!!! thnx a billion! – Daanvl Sep 16 '11 at 12:49
  • Is there a check I can do to know if I'm in a situation where I need to do `BeginInvoke` or not? – João Portela Apr 24 '14 at 13:58
  • @JoãoPortela yes: http://stackoverflow.com/questions/5143599/detecting-whether-on-ui-thread-in-wpf-and-winforms – thumbmunkeys Apr 24 '14 at 14:08
  • @thumbmunkeys I'm already on a UI thread just like OP, but I want to know If I'm in a rendering state that can cause an InvalidOperationException – João Portela Apr 24 '14 at 18:19
  • @JoãoPortela if you know that you are on the UI thread, then you dont need `BeginInvoke` – thumbmunkeys Apr 24 '14 at 18:22
  • 1
    @thumbmunkeys Like it says in your answer: the InvalidOperationException is not because we are not on the UI thread, it's because we are on the UI thread but trying to show a message box when the visual state tree is being altered. – João Portela May 08 '14 at 17:27