gpt4 book ai didi

C# Application.Restart() 启动程序两次

转载 作者:行者123 更新时间:2023-11-30 16:17:40 29 4
gpt4 key购买 nike

我遇到了一个奇怪的错误。我调用了 Application.Restart(); 方法,假设它将重新启动程序一次。但是,当我调用它时,它会重新启动并启动程序,而不是一次而是两次,从而在窗口上产生两种形式。为什么会这样?请帮忙!

这是我的代码:

Main.cs

private void Login_Load(object sender, EventArgs e)
{
// DO some checkings here to see if setting is needed

if (SettingIsNeeded)
{
SettingsForm Settings = new SettingsForm();
Settings.Show();
}
}

SettingsForm.cs

private void button1_Click(object sender, EventArgs e)
{
if (Settings.Default.COM != comboBox1.Text || Settings.Default.Gate_IP != textBox1.Text || Settings.Default.Server_Address != textBox2.Text) //Check if change has been made
{
Settings.Default.COM = comboBox1.Text;
Settings.Default.Gate_IP = textBox1.Text;
Settings.Default.Server_Address = textBox2.Text;
Settings.Default.Save();
settingschanged = true;
this.Close();
}
settingschanged = false;
this.Close();
}

private void COM_Settings_FormClosing(object sender, FormClosingEventArgs e)
{
if (settingschanged)
{
Application.Restart(); //This is where only the method will be called for restart.
}
}

最佳答案

我只是在这里猜测,但如果您尝试这样做会发生什么?

private bool isRestarted;

private void COM_Settings_FormClosing(object sender, FormClosingEventArgs e)
{
if (settingschanged && !isRestarted)
{
isRestarted = true;
Application.Restart(); //This is where only the method will be called for restart.
}
}

关于C# Application.Restart() 启动程序两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16880964/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com