문제

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