Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体:StackOverflowException_C#_Forms_Stack Overflow - Fatal编程技术网

C# Windows窗体:StackOverflowException

C# Windows窗体:StackOverflowException,c#,forms,stack-overflow,C#,Forms,Stack Overflow,我在VisualStudio2010中使用C#构建一个windows窗体。当我按下按钮时,我得到了StackOverflowException。我找到了一些替代方法来解决这个问题,但运气不好 Program是我创建的一个类,该类中有一个Execute函数,它执行所有的密集计算等等 private void execute_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(Execu

我在VisualStudio2010中使用C#构建一个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,我真的很困惑


请帮忙!谢谢

我假设new Program()创建程序,调用p.Execute(),它再次创建新程序()并调用p.Execute等等。。。无限递归会导致堆栈溢出。

我想new Program()创建程序,调用p.Execute(),它再次创建新程序()并调用p.Execute等等。。。无限递归导致堆栈溢出。

您似乎在从
Execute()
内部调用
Execute()
。这将导致堆栈不断增长,因为进程不断将新的Execute上下文置于彼此之上,在函数实际完成之前无休止地调用Execute。这会导致出现一个
StackOverflowException

您似乎正在从
Execute()
内部调用
Execute()
。这将导致堆栈不断增长,因为进程不断将新的Execute上下文置于彼此之上,在函数实际完成之前无休止地调用Execute。这会导致堆栈溢出异常

什么是p.function和p.inputfile?字段还是属性?请将这些代码添加到您的问题中。@ChrisBallard它们只是简单的字符串值。您可以发布程序类的代码吗?p.function和p.inputfile是什么?字段还是属性?请将这些代码添加到您的问题中。@ChrisBallard它们只是简单的字符串值。您能发布程序类的代码吗?我知道这有点混乱,但我在示例中使用的Execute()函数与p.Execute()不同,它们是两个不同的函数,所以它不会永远调用自己。请添加所有代码,否则我们只是猜测我知道这有点混乱,但我在示例中使用的Execute()函数与p不同。Execute(),它们是两个不同的函数,所以它不会永远调用自己。请添加所有代码,否则我们只是猜测