+-
C#Application.Restart()启动程序两次
我遇到了一个奇怪的错误.我调用了方法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()启动程序两次 - 乐贴网