window.ShowDialog stackoverflowexception reasons. WPF

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.


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.

Need Your Help

Finding normal distributions overlap using double Integral (dblquad) in MATLAB. Strange behaviour

matlab statistics normal-distribution integral

I am calculating the overlap of two normal bivariate distribution using the following function

Guard does not recognize Sublime Text 3 file changes

ruby-on-rails sublimetext guard

Currently, I am testing Sublime Text 3 (ST3) for my Rails projects.