【问题标题】:WinForms app not closing completelyWinForms 应用程序未完全关闭
【发布时间】:2015-04-29 08:14:40
【问题描述】:

这是我的程序课:

static class Program
{
    private class MyAppContext : ApplicationContext
    {
        public MyAppContext()
        {
            //Exit delegate
            Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

            //Show the main form
            MainForm main = new MainForm();
            main.Show();
        }

        private void OnApplicationExit(object sender, EventArgs e)
        {
            //Cleanup code...
        }
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyAppContext());
    }
}

当我在调试模式下运行应用程序并关闭它时,主窗体关闭,但应用程序仍在运行,我必须单击 Stop Debugging 才能完全终止它。

在主窗体中,我所做的只是:

protected override void OnFormClosed(FormClosedEventArgs e)
{
    base.OnFormClosed(e);
    Application.Exit();
}

我做错了什么?

【问题讨论】:

  • 您是否在代码中启动任何线程?
  • 所有表单关闭后需要调用ExitThread()。
  • @JonSkeet 没有启动线程,虽然我有一个带有HttpClient 实例的静态类。
  • 也就是说,您为什么使用ApplicationContext 模式?如果您不是特别需要它,最好只运行Application.Run(new MainForm());ApplicationContext 仅在您没有单一主表单时才真正有用 - 很明显这不是您的情况(因为无论如何您在 OnFormClosed 中调用 Application.Exit)。
  • @Ivan-MarkDebono 你不要那样关闭应用程序,这是不正确的

标签: c# winforms


【解决方案1】:

问题在于继承 AppContext,不知道为什么需要它。建议您在 MainForm_Closing、OnFormClosed、App_Exit 或类似事件中不使用 AppContext 和清理正常进行。

public class Program
{
    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new MyAppContext()); <- no need
        Application.Run(new MainForm());
    }
}

【讨论】:

    【解决方案2】:

    编辑:

    这对我来说很好。所以肯定是有其他问题。发布更多代码。

    public class Program
    {
        private class MyAppContext : ApplicationContext
        {
            public MyAppContext()
                : base(new Form())
            {
                //Exit delegate
                Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
    
                MainForm.Show();
            }
    
            private void OnApplicationExit(object sender, EventArgs e)
            {
                //Cleanup code...
            }
        }
    
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyAppContext());
        }
    }
    

    【讨论】:

    • 这个建议和上面的ExitThread() 建议都不起作用。
    • @Andrew:你省略了有价值的部分:错误来自这部分代码:protected override void OnFormClosed(FormClosedEventArgs e) { base.OnFormClosed(e);应用程序.Exit(); }
    【解决方案3】:

    试试看:

    protected override void OnFormClosed(FormClosedEventArgs e)
    {
        base.OnFormClosed(e);
        Environment.Exit(0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多