Thread: Application.Exit

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Application.Exit

    Hello everyone,


    I am new to this API. After some study, Application.Exit should only be used in Forms application, and in console/Windows service application without any GUI Window, we should not use it to exit application, right?


    thanks in advance,
    George

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I never use it in forms applications, since it makes the program NOT reach the end of the main function, it just exits. No proper cleanup.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Magos,


    You mean this function is not used to terminate a process (console and Windows service application)?

    Seems it just return from Run method.

    http://msdn2.microsoft.com/en-us/lib...63(VS.60).aspx

    Quote Originally Posted by Magos View Post
    I never use it in forms applications, since it makes the program NOT reach the end of the main function, it just exits. No proper cleanup.

    regards,
    George

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I mean if I have code like this:
    Code:
    Main()
    {
      try
      {
        Setup();
        System.Windows.Forms.Application.Run(...);
      }
      finally
      {
        Shutdown();
      }
    }
    And then, at some point, call Exit() while the form is open, the function Shutdown() is never called. This is what I found out through trial and error, I haven't really studied why. I just avoid calling Exit()
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Magos,


    You are developing Forms based application. My point is, for console application and Windows service based application, Application.Exit is useless, and should not be used to terminate the process (Application.Exit only used in Forms application, right?).

    Quote Originally Posted by Magos View Post
    I mean if I have code like this:
    Code:
    Main()
    {
      try
      {
        Setup();
        System.Windows.Forms.Application.Run(...);
      }
      finally
      {
        Shutdown();
      }
    }
    And then, at some point, call Exit() while the form is open, the function Shutdown() is never called. This is what I found out through trial and error, I haven't really studied why. I just avoid calling Exit()

    regards,
    George

  6. #6
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    24

    Exit();

    Application.Exit();

    Applies to any C# application as it is a base class type. It seems confusing at first but makes a great deal of sense as your exposure to the .Net Framework builds.

    With the Exit method call the .Net Framework takes care of disposal and cleanup for you automagically without the need for explicitly requesting any general aspects regarding the shutdown and release of your application's memory allocation (also known as garbage collection).

    The .Net methodology is centric on the term 'common.' Common functionality is shared among all like types, objects...applications.

    A console application is an application that outputs to the System.Console object and that is all. Therefore in retrospect Application.Exit(); is all you need for a user initiated destruction of the system. All attached processes originating from the application/point of entry will be disposed in the reverse order of their creation automagically by the .Net Framework.

    Dialogs and GUI Forms, that can also be created from a console application, can be closed from within themselves or via a callback to the application that requested their creation (parent process) using the dispose method allowing the parent application to stay up and running. The disposal method also invocates the .Net disposal methodologies and garbage collection services that an Application.Exit(); method call would invoke but isolated to the child process Dialog/Form of the parent process.

    No worries.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks TheRaven,


    Two more comments,

    1.

    I think for a non-GUI console application or Windows service application, Application.Exit does not terminate the process, right?

    2.

    About your comments below, could you show some pseudo code or give some reference link please?

    --------------------
    can be closed from within themselves or via a callback to the application that requested their creation (parent process) using the dispose method allowing the parent application to stay up and running.
    --------------------

    Quote Originally Posted by TheRaven View Post
    Application.Exit();

    Applies to any C# application as it is a base class type. It seems confusing at first but makes a great deal of sense as your exposure to the .Net Framework builds.

    With the Exit method call the .Net Framework takes care of disposal and cleanup for you automagically without the need for explicitly requesting any general aspects regarding the shutdown and release of your application's memory allocation (also known as garbage collection).

    The .Net methodology is centric on the term 'common.' Common functionality is shared among all like types, objects...applications.

    A console application is an application that outputs to the System.Console object and that is all. Therefore in retrospect Application.Exit(); is all you need for a user initiated destruction of the system. All attached processes originating from the application/point of entry will be disposed in the reverse order of their creation automagically by the .Net Framework.

    Dialogs and GUI Forms, that can also be created from a console application, can be closed from within themselves or via a callback to the application that requested their creation (parent process) using the dispose method allowing the parent application to stay up and running. The disposal method also invocates the .Net disposal methodologies and garbage collection services that an Application.Exit(); method call would invoke but isolated to the child process Dialog/Form of the parent process.

    No worries.

    regards,
    George

  8. #8
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    24

    Application.Exit();

    .Net handles the main() method that is considered to be the primary point of entry the ' Application Object ' and Application.Exit(); basically sends a term signal for this method. The application that contains this application object main() method is the parent or root system of your application. When you exit main the application is terminated and destroyed via garbage collection.

    Child objects of the application object are also destroyed, prior to the parent/application object, in reverse order of creation/instantiation.

    You should only dispose of child objects that were instantiated by the application object. If you use Application.Exit(); from a child object it is telling the system (.Net) to destroy the entire application object, closing down and exiting your application. When the Application.Exit(); method is executed the termination applies to the system from which it was instantiated regardless of the form or dialog that sends it.

    There are numerous examples that utilize this methodology at MSDN in the "How Do I" section of the site. Lots of examples, code, and tutorials as well as videos. Your best bet is to check out anything that involves creating/calling forms from within forms like: splash screens, non-model dialogs; etc.

    You start MyApp.
    using a button in MyApp that transmits the signal Application.Exit(); (like an EXIT button) closes the entire application and all of its child objects (.dll's, child forms; etc.)

    You start MyApp and open a dialog and that dialog has a button that says CLOSE that transmits the signal MyAppDialog.Dispose(); You click this button and it will shut down and exit the dialog and will not shut down and exit MyApp because all you have requested was that the Dialog that belongs to MyAPP be closed and exited.

    If your MyAPP Dialog has a button that transmits Application.Exit(); clicking this button will shutdown and exit the entire application not just the dialog. The dispose method is a granular disposal method for destroying objects without closing and exiting the main application.
    Last edited by TheRaven; 05-01-2008 at 04:31 PM.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks TheRaven,


    Your reply is great! All your replied is about how to use Application.Exit to destroy something, but why do you say -- "the parent application to stay up and running"? Application.Exit could be used to keep application running? :-)

    Quote Originally Posted by TheRaven View Post
    .Net handles the main() method that is considered to be the primary point of entry the ' Application Object ' and Application.Exit(); basically sends a term signal for this method. The application that contains this application object main() method is the parent or root system of your application. When you exit main the application is terminated and destroyed via garbage collection.

    Child objects of the application object are also destroyed, prior to the parent/application object, in reverse order of creation/instantiation.

    You should only dispose of child objects that were instantiated by the application object. If you use Application.Exit(); from a child object it is telling the system (.Net) to destroy the entire application object, closing down and exiting your application. When the Application.Exit(); method is executed the termination applies to the system from which it was instantiated regardless of the form or dialog that sends it.

    There are numerous examples that utilize this methodology at MSDN in the "How Do I" section of the site. Lots of examples, code, and tutorials as well as videos. Your best bet is to check out anything that involves creating/calling forms from within forms like: splash screens, non-model dialogs; etc.

    You start MyApp.
    using a button in MyApp that transmits the signal Application.Exit(); (like an EXIT button) closes the entire application and all of its child objects (.dll's, child forms; etc.)

    You start MyApp and open a dialog and that dialog has a button that says CLOSE that transmits the signal MyAppDialog.Dispose(); You click this button and it will shut down and exit the dialog and will not shut down and exit MyApp because all you have requested was that the Dialog that belongs to MyAPP be closed and exited.

    If your MyAPP Dialog has a button that transmits Application.Exit(); clicking this button will shutdown and exit the entire application not just the dialog. The dispose method is a granular disposal method for destroying objects without closing and exiting the main application.

    regards,
    George

Popular pages Recent additions subscribe to a feed