tags:

views:

1288

answers:

5

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.

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.

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);
    }

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.

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

A: 

Is this application run (in the Main method) using Application.Run()? Otherwise, Application.Exit() won't work.

If you wrote your own Main method and you want to stop the application, you can only stop by returning from the Main method (or killing the process).

configurator
+1  A: 

Try Environment.Exit(exitCode).

Nick Gunn
+7  A: 

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/

Basically:

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

Another discussion of this: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

This article points out a good tip:

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)
}
Geoffrey Chetwood
Thanks. I probably didn't call Application.Run or something
Tony Peterson
I added an additional return after the call to the exit method. That fixed the problem.
Tony Peterson
A: 

I have went though this situation in many cases I use Thread.CurrentThread.Abort()

and the process is closed. It seems that Application.Exit isn't hooking up properly with current thread.

Ahmet Murati
A: 

Also ensure any threads running in your application have the IsBackground property set to true. Non-background threads will easily block the application from exiting.

Andrew