为什么会Application.Exit无法正常工作?无法正常、工作、Application、Exit

2023-09-02 21:28:29 作者:傻瓜ゞ我已与悲傷、有約叻

我已经取消了对话框时,已收到奇怪的错误,一个应用程序。应用程序不能继续,如果框被抵消,所以它退出,但它不工作由于某种原因,因此其保持运行和崩溃。

I have an application that has been getting strange errors when canceling out of a dialog box. The application can't continue if the box is cancelled out of, so it exits, but it is not working for some reason, and thus it keeps running and crashes.

我调试这个问题,并以某种方式在应用程序运行的权利过去Application.Exit电话。我在调试模式下运行,这是因为少量的code,它依赖于所定义的释放变量相关。这里是我的应用程序退出code。我已经追查code和它进入方法ExitApp命令,并保持下去,将控制权返回给调用者,并最终崩溃。

I debugged this problem, and somehow the application runs right past the Application.Exit call. I'm running in Debug mode, and this is relevant because of a small amount of code that depends on the RELEASE variable being defined. Here is my app exit code. I have traced the code and it entered the ExitApp method, and keeps on going, returning control to the caller and eventually crashing.

这是一个应用程序,它提供了远程桌面连接的报道,所以这就是为什么退出code是有点怪。它试图终止远程会话,但只有在发布运行时,因为我不想关机我的dev的机器,每试运行。

This is an application which provides reports over a remote desktop connection, so that's why the exit code is a bit weird. Its trying to terminate the remote session, but only when running under release because I don't want to shut down my dev machine for every test run.

    private void ExitApp()
    {
        HardTerminalExit();
        Application.Exit();
    }

    // When in Debug mode running on a development computer, this will not run to avoid shutting down the dev computer
    // When in release mode the Remote Connection or other computer this is run on will be shut down.
    [Conditional("RELEASE")]
    private void HardTerminalExit()
    {
        WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
    }

我已经运行调试器右侧过去的Application.Exit行并没有任何反应,则控制返回给调用者后,我踩过去该行。

I've run a debugger right past the Application.Exit line and nothing happens, then control returns to the caller after I step past that line.

这是怎么回事?这是一个Windows窗体应用程序。

What's going on? This is a Windows Forms application.

推荐答案

这是一篇文章,其中扩展了同样的想法,你正在经历的思路:http://www.dev102.com/2008/06/24/how-do-you-exit-your-net-application/

This is an article which expands on the same train of thought you are going through: http://www.dev102.com/2008/06/24/how-do-you-exit-your-net-application/

基本上是:

  

Environment.Exit - 从MSDN:终止这个过程,并给出了   底层的操作系统   指定的退出code。这是code   当您使用控制台打电话   应用程序。

Environment.Exit - From MSDN: Terminates this process and gives the underlying operating system the specified exit code. This is the code to call when you are using console application.

Application.Exit - 从MSDN:通知所有消息泵   必须终止,然后关闭所有   消息后,应用程序窗口   已经被处理。这是code   使用,如果你正在呼吁   Application.Run(的WinForms   应用程序),这个方法停止所有   运行的消息循环的所有线程   并关闭的所有窗口   应用。还有一些更   有关此方法的问题,阅读   它在MSDN页面。

Application.Exit - From MSDN: Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This is the code to use if you are have called Application.Run (WinForms applications), this method stops all running message loops on all threads and closes all windows of the application. There are some more issues about this method, read about it in the MSDN page.

这样做的另一个讨论:http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

该文章指出,一个很好的提示:

This article points out a good tip:

您可以决定是否System.Windows.Forms.Application.Run已被称为检查System.Windows.Forms.Application.MessageLoop财产。如果为true,则运行已经打来电话,你可以假设一个WinForms应用程序执行如下:

You can determine if System.Windows.Forms.Application.Run has been called by checking the System.Windows.Forms.Application.MessageLoop property. If true, then Run has been called and you can assume that a WinForms application is executing as follows.

if (System.Windows.Forms.Application.MessageLoop)
{
  // Use this since we are a WinForms app
  System.Windows.Forms.Application.Exit();
}
else
{
  // Use this since we are a console app
  System.Environment.Exit(1);
}