我正在使用C#在Visual Studio 2010中构建一个Windows窗体。当我按下按钮时,我收到了StackOverflowException。我找到了一些替代方法来解决这个问题,但没有运气。
Program
是我创建的一个类,在该类中有一个Execute
函数正在执行所有强烈的计算等。
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
}
当我运行它时,控制台只打印出1111.我真的很困惑如何分配值可以创建StackOverflowException。
请帮忙!谢谢!
答案 0 :(得分:3)
我想新的Program()创建程序,调用p.Execute()再次创建新的Program()并调用p.Execute等等......无限递归导致堆栈溢出。
答案 1 :(得分:3)
您似乎是从Execute()
内拨打Execute()
。这将导致不断增长的堆栈,因为进程不断将新的Execute上下文放在彼此之上,在函数实际完成之前无休止地调用Execute。这导致StackOverflowException