Question: 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.

Answer: 
Having had this problem recently (that Application.Exit was failing to correctly terminate message pumps for win-forms with Application.Run(new Form())), I discovered that if you are spawning new threads or starting background workers within the constructor, this will prevent Application.Exit from running.

Move all 'RunWorkerAsync' calls from the constructor to a form Load method:

```
public Form()
{
  this.Worker.RunWorkerAsync();
}

```

Move to:

```
public void Form_Load(object sender, EventArgs e)
{
  this.Worker.RunWorkerAsync();
}

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