Reputation: 355
Apologies if this is a duplicate, but the name of your website really doesn't help when I'm trying to Google what's happening here.
So, I have my MainWindow--which has been working fine as an application up until this point; I am just testing some simple behaviour.
Now, I wanted to add another window, which will open to specify some settings for an operation. The design and function of that isn't so much the issue as is getting it to actually appear and open without the application hanging.
So, I did Add > New Window in Visual Studio, to my project, and added a new WPF window (I am using WPF for this application). Called it Window1. Window1.xaml:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1
{
public Window1()
{
InitializeComponent();
Window win1 = new Window1();
win1.Close();
}
}
}
The idea is that it closes on startup because it's supposed to be form which only appears when a button is pressed. I do not, however, feel this is the problem. What is, is this:
private void AddNewThingButton_Click(object sender, RoutedEventArgs e)
{
Window1 win1 = new Window1();
win1.Show();
}
The application does not hang at startup, it hangs and gives a stack overflow error when I press the button to which this event is connected. I am using the correct namespace--the program compiles.
The full details of the error are as follows:
When the button is pressed, the application hangs for 2-3 seconds and the loading circle is present over the application. After this period, a warning message box comes up and says:
"An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll. If the program has a handler for this exception, it may safely continue."
All of the options on there (Break, Continue, Exit) seem to do the same thing: eventually close the program.
I must be doing something really wrong to fail to do something as basic as this--however, I can't find what that is. Can anyone help me here?
Upvotes: 2
Views: 135
Reputation: 564383
The problem is that your Window1
constructor creates a new Window1
instance, which in turn creates another, which in turn creates another:
public Window1()
{
InitializeComponent();
// This shouldn't be here!
// Window win1 = new Window1();
// win1.Close();
}
You don't need to "close it on startup", since it's only being created and shown when you click the button.
Upvotes: 4