Application.Exit vs. Environment.Exit

While refining my unhandled exception handler code, I wandered into this question.  What is the real difference between the following three statements:

  System.Environment.Exit(-1)
  System.Windows.Forms.Application.Exit()
  End ' Visual Basic only

They can all get you to where you want to go, application shutdown, but when is each appropriate?  There is a brief bit buried in the C# FAQ.  Looking through the Microsoft help is not particularly helpful, but it does give you the rigorous description of what each does.

  • System.Windows.Forms.Application.Exit() - Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.  This method stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.  This is the call to use if you are running a WinForms application.  As a general guideline, use this call if you have called System.Windows.Forms.Application.Run.

  • System.Environment.Exit(exitCode) - Terminates this process and gives the underlying operating system the specified exit code.  This call requires that you have SecurityPermissionFlag.UnmanagedCode permissions. If you do not, a SecurityException error occurs.  This is the call to use if you are running a console application.

  • End - (Visual Basic only)  Terminates execution immediately.  The End statement can be placed anywhere in a procedure to end code execution, close files opened with an Open statement, and clears variables at module and class level and all static local variables in all modules.  The End statement calls System.Environment.Exit.  The End statement stops code execution abruptly, without invoking the Finalize method or any other Visual Basic code.  Object references held by other programs are invalidated.  The End statement provides a way to force your program to halt.  For normal termination of a Visual Basic program, you should unload all forms.  Your program closes as soon as there are no other programs holding references to objects created and no code executing.

What do you do in a reusable library when you do not know how it will be used?  First, in a VB program, do not use End.  This will likely lead to undesireable results.

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

This article is part of the GWB Archives. Original Author: Mark Treadwell

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.