C# 单击编程按钮会引发“System.StackOverflowException”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2271883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:56:18  来源:igfitidea点击:

Programmatic button click throws 'System.StackOverflowException' exception

c#buttonclickstack-overflowsimulate

提问by Crazyd22

I have written a WinForms program in C#.Net to click a button programmatically within a password form.

我在 C#.Net 中编写了一个 WinForms 程序,以在密码表单中以编程方式单击按钮。

Form1loads and shows Form2as a dialogue box.

Form1加载并显示Form2为对话框。

The application will close if DialogResult is anything other that DialogResult.OK.

如果 DialogResult 不是 DialogResult.OK,则应用程序将关闭。

So far I have a button click event, which is coded as follows:

到目前为止,我有一个按钮单击事件,其编码如下:

 if (txtpass.Text == "")
            {
                MessageBox.Show("You need to enter a password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtpass.Focus();
            }
            else
            {
                if (txtpass.Text == "1234")
                {
                    radButton1.DialogResult = DialogResult.OK;
                    radButton1.PerformClick();
                }
                else
                {
                    MessageBox.Show("Password Incorrect", "Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtpass.Text = "";
                    txtpass.Focus();
                }
            }

I use radButton1.PerformClick();, but running the program gives me the following message:

我使用radButton1.PerformClick();,但运行该程序会给我以下消息:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

I'm unsure what is causing this exception to throw.

我不确定是什么导致了这个异常抛出。

采纳答案by jjnguy

EditNot a guess. Telling the button to click itself from within itself is most definitely causing an infinite loop. This causes the method to get called over and over, filling up the stack and causing it to overflow.

编辑不是猜测。告诉按钮从自身内部单击自身肯定会导致无限循环。这会导致该方法被一遍又一遍地调用,填满堆栈并导致它溢出。

My guess is that calling PerformClick()is causing the current method you posted to get called again, thus causing an infinite call loop and resulting in a StackOverflowException.

我的猜测是调用PerformClick()导致您发布的当前方法再次被调用,从而导致无限调用循环并导致StackOverflowException.

To prevent this, you need to fix the logic somewhere in your code so that:

为了防止这种情况,您需要修复代码中某处的逻辑,以便:

if (txtpass.Text == "1234")

evaluates to falseand the click method doesn't get called over and over. You can probably achieve this by setting txtpass.Text = ""right before you cause it to click itself again.

评估为false并且 click 方法不会被一遍又一遍地调用。您可以通过txtpass.Text = ""在使其再次单击之前进行设置来实现此目的。

回答by herzmeister

To call the event handler again from inside you could use the following code:

要从内部再次调用事件处理程序,您可以使用以下代码:

if (txtpass.Text)
{
    case "1234":
        radButton1.DialogResult = DialogResult.OK;

        txtpass.Text = "12345";

        radButton1.PerformClick();

        break;

    default:
        case "12345":
        break;

}

回答by Ian

Normally you would manually call the event that you are trying to run.

通常,您会手动调用您尝试运行的事件。

E.g. if you have a method

例如,如果你有一个方法

button1_Click(object sender, ButtonEventArgs e)
{
}

Then you would call the following in your code:

然后你会在你的代码中调用以下内容:

button1_Click(this, new ButtonEventArgs());

I think maybe you need to explain some logic in your code though, as it's not clear what you're trying to do. The StackOverflow probably because you're doing

我想也许您需要在代码中解释一些逻辑,因为不清楚您要做什么。StackOverflow 可能是因为你在做

PerformClick() -> PerformClick() -> PerformClick() because your "1234" text never changes between calls.

PerformClick() -> PerformClick() -> PerformClick() 因为您的“1234”文本在调用之间永远不会改变。

回答by Andy Shellam

Is the PerformClick()inside the button's click event? If so, that's where you're going wrong because you're throwing your application into an infinite loop.

PerformClick()按钮的点击事件里面吗?如果是这样,这就是您出错的地方,因为您将应用程序投入到无限循环中。

User clicks button,
.NET runs Click() handler,
Button clicks PerformClick(),
.NET runs Click() handler,
Button clicks PerformClick(),
.NET runs Click() handler,
Button clicks PerformClick(),

用户点击按钮,
.NET 运行 Click() 处理程序,
按钮点击PerformClick()
.NET 运行 Click() 处理程序,
按钮点击PerformClick()
.NET 运行 Click() 处理程序,
按钮点击PerformClick()

etc.

等等。

Is form1definitely calling ShowDialog()on form2, and not just Show()?

form1绝对调用ShowDialog()form2,而不是仅仅Show()

Instead of radButton1.DialogResult, try setting this.DialogResult == DialogResult.OK.

而不是radButton1.DialogResult,尝试设置this.DialogResult == DialogResult.OK

The DialogResultproperty on a button tells .NET which DialogResultto assign to the Formwhen the Buttonis clicked.

DialogResult按钮上的属性告诉 .NET在单击时将哪个DialogResult分配给。FormButton

回答by WonderWorker

A stack overflow happens usually because method is indefinitely calling itself, because each time a method is called, an entry is added to the stack to the point where there is no more stack left.

堆栈溢出的发生通常是因为方法无限期地调用自身,因为每次调用一个方法时,都会向堆栈中添加一个条目,直到没有更多堆栈为止。

To stop the recursion, remove the line radButton1.PerformClick();

要停止递归,请删除该行 radButton1.PerformClick();