3

我遇到了一个奇怪的错误。我调用了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.
        }
    }
4

3 回答 3

2

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

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.
    }
}
于 2013-06-02T08:32:44.920 回答
1

return;在第一个之后添加一个this.Close();

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

更新

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

于 2013-06-02T08:19:12.760 回答
0

我想在这里添加一些发现。您可以按照Matthew的建议进行操作,但我确实认为您的代码存在一些问题。我的两分钱是打电话RestartFormClosing而只是触发Start(例如Process.Start

为什么你有两个应用程序启动?
似乎是因为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两次,意思是 twoStarts和 two Exits。但由于只有一个程序要退出,我猜其中一个被忽略(而不是例如抛出异常)。

例如,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)
于 2013-06-02T08:53:35.920 回答