0

What is main reasons for window.ShowDialog() stackOverflowException in WPF? I receive this exception after 10-20 seconds when I call:

if(myWindow.ShowDialog() == true)
{
   //other stuff
}

Window is shows up and works good, but then I receive this exception.

5
  • Do you have some code in myWindow Shown event? Commented Nov 30, 2011 at 14:15
  • Do you have this code within an event handler which is called as part of showing the dialog by any chance? Commented Nov 30, 2011 at 14:16
  • This window called when button is pressed. And Yes, i have code in this window, but I didn't find there recursion or something like this. Commented Nov 30, 2011 at 14:19
  • 1
    if this code is in the form_loaded of myWindow i think i found your problem Commented Nov 30, 2011 at 14:45
  • hcb, yes, there is code. I call method that updates text on buttons. Commented Nov 30, 2011 at 15:05

2 Answers 2

2

The generic cause of an SOE like this is having an event handler whose code causes the same event to be raised again. A simple example is:

    private void textBox1_TextChanged(object sender, TextChangedEventArgs e) {
        textBox1.Text += "a";
    }

Type a letter, takes about 5 seconds for program to run out of stack space and bomb. Your primary weapon to diagnose exactly which event handler causes this problem is the debugger, look at the Call Stack window. You solve it by using a little helper variable that indicates that you expect the event to be fired again so you can ignore it. Like this:

    bool changingText;

    private void textBox1_TextChanged(object sender, TextChangedEventArgs e) {
        if (changingText) return;
        changingText = true;
        try {
            textBox1.Text += "a";
        }
        finally {
            changingText = false;
        }
    }

The try/finally is not strictly necessary but wise if you expect to keep your program running after an exception.

Sign up to request clarification or add additional context in comments.

2 Comments

I call this code from timer: int response = MSR_Get_Write_Erase_Status(); if (response == 0) return; else { ` //do something` } Can this be the reason of an exception? I disable timer at the end of else code block
Unlikely but impossible to guess. Use the debugger as I suggested.
0

Surprisingly a stack overflow exception can be caused by repeatedly calling window.ShowDialog asynchronously.

    public MainWindow()
    {
        InitializeComponent();
        TheCallDelegate = TheCall;
        _timer = new DispatcherTimer();
        _timer.Tick += _timer_Tick;
        _timer.Start();
    }

    DispatcherTimer _timer = null;

    void _timer_Tick(object sender, EventArgs e)
    {
        _timer.Dispatcher.BeginInvoke(TheCallDelegate);            
    }

    Action TheCallDelegate;

    void TheCall()
    {
        Window win = new Window();
        win.ShowDialog();
    }

As you can see there is no actual recursion here (or there shouldn't have been) but once the exception happens you can see that the call stack is indeed full. Without looking at how Window.ShowDialog is implemented internally I can't say what is the deeper cause of this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.