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

C#Application.Restart()启动程序两次,c#,C#,我遇到了一只奇怪的虫子。我调用了方法Application.Restart()假设它将重新启动程序一次。但是,当我调用它时,它会重新启动并启动程序,不是一次而是两次,在窗口上生成两个窗体。为什么会这样?请帮忙 这是我的密码: Main.cs private void Login_Load(object sender, EventArgs e) { // DO some checkings here to see if setting is needed if (Set

我遇到了一只奇怪的虫子。我调用了方法
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.
        }
    }

添加一个
返回this.Close()之后的code>

可能调用Close()两次也会触发FormClosing处理程序两次?(只是猜测)

更新


使用
Application.Restart()
的正确方法是从Program.Main()调用它,如中所示。

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

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.
    }
}

我想在这里补充一些发现。您可以按照建议执行,但我确实认为您的代码存在一些问题。 我的两分钱是不调用
表单关闭中的
重启
,而只是触发一个
启动
(例如
进程.启动

为什么要启动两次应用程序?
这似乎是因为
Application.Restart()
也会触发
FormClosing
事件。调用
Application.Restart()
的堆栈跟踪是:

new StackTrace();
{   at WindowsFormsApplication1.Form1.Form1_FormClosing(Object sender, FormClosingEventArgs e)
   at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
   at System.Windows.Forms.Form.RaiseFormClosingOnAppExit()
   at System.Windows.Forms.Application.ExitInternal()
   at System.Windows.Forms.Application.Restart()
   at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e)
Close()
的堆栈跟踪是

new StackTrace();
{   at WindowsFormsApplication1.Form1.Form1_FormClosing(Object sender, FormClosingEventArgs e)
   at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
因此,您确实会得到两次
FormClosing
事件,这将触发两次
Restart
,这意味着两次
启动
和两次
退出
。但由于只有一个程序要退出,我想其中一个程序会被忽略(而不是抛出一个异常)

例如,如果在
FormClosing
中调用
Close()
,则需要输入两次
FormClosing

new StackTrace();
{   at WindowsFormsApplication1.Form1.Form1_FormClosing(Object sender, FormClosingEventArgs e)
   at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
   at System.Windows.Forms.Form.Close()
   at WindowsFormsApplication1.Form1.Form1_FormClosing(Object sender, FormClosingEventArgs e)

也许你会叫它两次。我们可以看到您的代码吗?也需要发布相关代码,右键单击某个应用程序。重新启动并查找所有引用,然后发布这些方法如果相关或原始实例阻止关闭它的尝试可能是因为您没有正确处理表单,请使用using@Sayse,你能提供一些例子吗?谢谢@Matthew-与默认值类似,我也进行了一些研究,得出了相同的结论,而不是使用一个新的布尔值,您可以执行
settingsChanged&&e.CloseReason!=CloseReason.ApplicationExitCall
谢谢-这个codeproject示例对我来说是一个解决方案