Reputation: 2378
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!
Upvotes: 1
Views: 1077
Reputation: 636
I was facing the same exception but my answer might help someone else. I was using therads, timers, backgroundworker thread and graphics objects. So i was thinking that they are the culprits but once i removed Random() method. This exception stopped.
Upvotes: 0
Reputation: 1171
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
Upvotes: 3
Reputation: 1474
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.
Upvotes: 4