Pergunta

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!

Foi útil?

Solução

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.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top