I was building a windows form in Visual Studio 2010 using C#. I was getting the StackOverflowException when I hit the button. I found some alternative ways to solve this but had no luck.

Program is a class I created, and there's a Execute function within that class that's doing all the intense calculation and such.

private void execute_Click(object sender, EventArgs e)
{
    Thread thread = new Thread(new ThreadStart(Execute));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

private void Execute()
{
    Console.WriteLine("1111");
    Program p = new Program();
    p.function = "myNewFunction";
    p.inputfile = fileTextbox.Text;
    Console.WriteLine("2222");
    p.Execute(); //somehow never reaches here
}

When I run it, the console only prints out 1111. I'm really confused as to how assigning values could create StackOverflowException.

Please help! Thanks!

有帮助吗?

解决方案

I suppose new Program() creates program, calls p.Execute() which again creates new Program() and call p.Execute and so on... infinite recursion leads to stack overflow.

其他提示

You appear to be calling Execute() from within Execute(). This will result in a continuously growing stack as the process keeps placing new contexts of Execute on top of each other, endlessly calling Execute before the function actually finishes. This leads to a StackOverflowException

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top