Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would Application.Exit fail to work?

Tags:

c#

.net

winforms

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.

like image 491
Tony Peterson Avatar asked Feb 16 '09 20:02

Tony Peterson


People also ask

What does application exit do?

The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return.

What is the purpose of using below command application exit ();?

Application. Exit( )". This method internally informs all message loops in application that you must terminate. Then this method wait to close all application windows till the message loops have been processed.

How do you exit a program in Visual Basic?

Just Close() all active/existing forms and the application should exit. ok.


2 Answers

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); } 
like image 78
GEOCHET Avatar answered Sep 29 '22 12:09

GEOCHET


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(); } 
like image 43
StigM Avatar answered Sep 29 '22 12:09

StigM