当用户从文件菜单中单击退出菜单项时,应该如何退出应用程序?

我试过:

this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();

还有很多其他的。没有什么工作。


当前回答

if (System.Windows.Forms.Application.MessageLoop) 
{
    // WinForms app
    System.Windows.Forms.Application.Exit();
    system.Environment.Exit(1);
}

其他回答

Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

根据需要使用以下任何一种方法:

1.

 App.Current.Shutdown();
OR
 Application.Current.Shutdown();

2.

 App.Current.MainWindow.Close();
OR
 Application.Current.MainWindow.Close();

以上所有的方法都会调用Window类的关闭事件,并且执行可能会在某个时候停止(因为通常应用程序会设置像'are you sure?或“关闭前要保存数据吗?”,在窗口完全关闭之前)

3.但如果您想立即终止应用程序而不发出任何警告。使用下面的

Environment.Exit(0);

App.Current.Shutdown (); 把它写在你需要的地方。

如果你想从另一个没有创建应用程序对象的线程中退出,请使用:

if (System.Windows.Forms.Application.MessageLoop) 
{
    // WinForms app
    System.Windows.Forms.Application.Exit();
    system.Environment.Exit(1);
}