Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Application.Exit

Application.Exit

Scheduled Pinned Locked Moved C#
jsonquestion
21 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    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

    U L P 3 Replies Last reply
    0
    • G George_George

      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

      U Offline
      U Offline
      Urs Enzler
      wrote on last edited by
      #2

      Hi George Application.Exit stops th emessage loop and closes all windows. Therefore it is not useful in console applications. Note that Application.Exit does not end the application, it just causes that the Application.Run (normally in Main) returns. See this link to MSDN: http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.exit(VS.71).aspx[^] Urs

      -^-^-^-^-^-^-^- no risk no funk

      G 1 Reply Last reply
      0
      • U Urs Enzler

        Hi George Application.Exit stops th emessage loop and closes all windows. Therefore it is not useful in console applications. Note that Application.Exit does not end the application, it just causes that the Application.Run (normally in Main) returns. See this link to MSDN: http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.exit(VS.71).aspx[^] Urs

        -^-^-^-^-^-^-^- no risk no funk

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Cool, Urs! I think the answer is, in console and Windows service application, Aplication.Exit has no use and it is not used to cause process to terminate, right? regards, George

        U 1 Reply Last reply
        0
        • G George_George

          Cool, Urs! I think the answer is, in console and Windows service application, Aplication.Exit has no use and it is not used to cause process to terminate, right? regards, George

          U Offline
          U Offline
          Urs Enzler
          wrote on last edited by
          #4

          Application.Exit will never terminate the process. Normally you have something like this in your winforms app:

          public void Main()
          {
          MyForm form = new MyForm();
          Application.Run(form);
          }

          Application.Exit will cause that the Application.Run method returns to the Main method and then, of course, the process will end, because nothing is done afterwards. best regards Urs

          -^-^-^-^-^-^-^- no risk no funk

          G 1 Reply Last reply
          0
          • U Urs Enzler

            Application.Exit will never terminate the process. Normally you have something like this in your winforms app:

            public void Main()
            {
            MyForm form = new MyForm();
            Application.Run(form);
            }

            Application.Exit will cause that the Application.Run method returns to the Main method and then, of course, the process will end, because nothing is done afterwards. best regards Urs

            -^-^-^-^-^-^-^- no risk no funk

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Thanks Urs, So, for console application and Windows service application without GUI, Application.Exit is useless, right? regards, George

            M 1 Reply Last reply
            0
            • G George_George

              Thanks Urs, So, for console application and Windows service application without GUI, Application.Exit is useless, right? regards, George

              M Offline
              M Offline
              MarkB777
              wrote on last edited by
              #6

              If there is no Application.Run process running, then the Application.Exit function is useless. As far as i'm aware your statement is true. Cheers,

              Mark Brock Click here to view my blog

              G 1 Reply Last reply
              0
              • M MarkB777

                If there is no Application.Run process running, then the Application.Exit function is useless. As far as i'm aware your statement is true. Cheers,

                Mark Brock Click here to view my blog

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Cool, Mark! Question answered. regards, George

                1 Reply Last reply
                0
                • G George_George

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi George, A lot of applications don't need Application.Exit(), even Windows Forms applications can do without if one wants them to only terminate when the main form gets closed. Service means no forms, means no Application.Exit(). I trust you are aware of Environment.Exit()? :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                  G 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi George, A lot of applications don't need Application.Exit(), even Windows Forms applications can do without if one wants them to only terminate when the main form gets closed. Service means no forms, means no Application.Exit(). I trust you are aware of Environment.Exit()? :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #9

                    Yes, Luc! Currently, I am using Environment.Exit (-1). And I want to keep on learning new things. Is Environment.Exit safe that no resource leak will occur (and all expected exception handler and finally blocks are executed)? regards, George

                    L 1 Reply Last reply
                    0
                    • G George_George

                      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

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      I never use Application.Exit; I just call the form's Close method.

                      G 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        I never use Application.Exit; I just call the form's Close method.

                        G Offline
                        G Offline
                        George_George
                        wrote on last edited by
                        #11

                        Thanks PIEBALDconsult, I just want to confirm in console and Windows service application, we should not use Application.Exit to terminate the process, right? regards, George

                        1 Reply Last reply
                        0
                        • G George_George

                          Yes, Luc! Currently, I am using Environment.Exit (-1). And I want to keep on learning new things. Is Environment.Exit safe that no resource leak will occur (and all expected exception handler and finally blocks are executed)? regards, George

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          Hi George, 1. yes Environment.Exit() will clean up and avoid leaks. Everything that terminates a process will clean up the resources allocated by the process itself, that is basic functionality in Windows itself. Nevertheless, it is good practice to do it explicitly; and you should clean up if your program has two or more parts that are rather independent, so after part 1 you could and should clean up before starting part 2, so part 2 gets a maximum of available resources (memory, handles, whatever) and hence gets the best performance. 2.

                          George_George wrote:

                          finally blocks are executed)?

                          Why don't you try it? you really should replace some of your questions by looking it up yourself and/or testing it yourself. Here is enough code to figure it out (put it in e.g. a button click handler):

                          try {
                          Application.Exit();
                          } finally {
                          StreamWriter tw=File.CreateText("finally.txt");
                          tw.Close();
                          }

                          :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                          G 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Hi George, 1. yes Environment.Exit() will clean up and avoid leaks. Everything that terminates a process will clean up the resources allocated by the process itself, that is basic functionality in Windows itself. Nevertheless, it is good practice to do it explicitly; and you should clean up if your program has two or more parts that are rather independent, so after part 1 you could and should clean up before starting part 2, so part 2 gets a maximum of available resources (memory, handles, whatever) and hence gets the best performance. 2.

                            George_George wrote:

                            finally blocks are executed)?

                            Why don't you try it? you really should replace some of your questions by looking it up yourself and/or testing it yourself. Here is enough code to figure it out (put it in e.g. a button click handler):

                            try {
                            Application.Exit();
                            } finally {
                            StreamWriter tw=File.CreateText("finally.txt");
                            tw.Close();
                            }

                            :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #13

                            Cool, Luc! Finally block is not executed. I think the reason is, process is terminated by Environment.Exit, and resources are released by Windows, so there is no need to execute finally block, right? regards, George

                            L 1 Reply Last reply
                            0
                            • G George_George

                              Cool, Luc! Finally block is not executed. I think the reason is, process is terminated by Environment.Exit, and resources are released by Windows, so there is no need to execute finally block, right? regards, George

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              Hi George,

                              George_George wrote:

                              Finally block is not executed

                              :confused::confused: It was executed in my experiment, with the code snippet in a button click handler, the file got generated in \bin\debug folder. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                              G 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Hi George,

                                George_George wrote:

                                Finally block is not executed

                                :confused::confused: It was executed in my experiment, with the code snippet in a button click handler, the file got generated in \bin\debug folder. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #15

                                Hi Luc, can you post your code please? Finally block is not executed, here is my code.

                                using System;
                                using System.Collections.Generic;
                                using System.Linq;
                                using System.Text;

                                namespace ConsoleApplication2
                                {
                                class Program
                                {

                                    static void Main(string\[\] args)
                                    {
                                        try
                                        {
                                            Environment.Exit(-1);
                                        }
                                        finally
                                        {
                                            Console.WriteLine("Hello world");
                                        }
                                    }
                                }
                                

                                }

                                regards, George

                                L 1 Reply Last reply
                                0
                                • G George_George

                                  Hi Luc, can you post your code please? Finally block is not executed, here is my code.

                                  using System;
                                  using System.Collections.Generic;
                                  using System.Linq;
                                  using System.Text;

                                  namespace ConsoleApplication2
                                  {
                                  class Program
                                  {

                                      static void Main(string\[\] args)
                                      {
                                          try
                                          {
                                              Environment.Exit(-1);
                                          }
                                          finally
                                          {
                                              Console.WriteLine("Hello world");
                                          }
                                      }
                                  }
                                  

                                  }

                                  regards, George

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Hi George, you already have my code[^]. I am not surprised your code snippet is fooling you, after all you ask it to perform console I/O where the application is exiting, hence forms, consoles, and other UI stuff is winding down. The file system however remains alive to the very end of the app. :)

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                  G 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    Hi George, you already have my code[^]. I am not surprised your code snippet is fooling you, after all you ask it to perform console I/O where the application is exiting, hence forms, consoles, and other UI stuff is winding down. The file system however remains alive to the very end of the app. :)

                                    Luc Pattyn [Forum Guidelines] [My Articles]


                                    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                    G Offline
                                    G Offline
                                    George_George
                                    wrote on last edited by
                                    #17

                                    Sorry Luc, Here is your code, the finally blocked is not executed. Could you reproduce? I am confused. :-)

                                    using System;
                                    using System.IO;
                                    using System.Collections.Generic;
                                    using System.Linq;
                                    using System.Text;

                                    namespace ConsoleApplication2
                                    {
                                    class Program
                                    {

                                        static void Main(string\[\] args)
                                        {
                                            try
                                            {
                                                Environment.Exit(-1);
                                            }
                                            finally
                                            {
                                                StreamWriter tw = File.CreateText("finally.txt");
                                                tw.Close();
                                            }
                                        }
                                    }
                                    

                                    }

                                    regards, George

                                    L 1 Reply Last reply
                                    0
                                    • G George_George

                                      Sorry Luc, Here is your code, the finally blocked is not executed. Could you reproduce? I am confused. :-)

                                      using System;
                                      using System.IO;
                                      using System.Collections.Generic;
                                      using System.Linq;
                                      using System.Text;

                                      namespace ConsoleApplication2
                                      {
                                      class Program
                                      {

                                          static void Main(string\[\] args)
                                          {
                                              try
                                              {
                                                  Environment.Exit(-1);
                                              }
                                              finally
                                              {
                                                  StreamWriter tw = File.CreateText("finally.txt");
                                                  tw.Close();
                                              }
                                          }
                                      }
                                      

                                      }

                                      regards, George

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #18

                                      Hi George, my example was on Application.Exit() which causes an Application.Run() to return, I guess asynchronously by sending a Windows message or something; so the thread has the opportunity to first execute the finally block. Your test was on Environment.Exit() which, from your observation, seems to cause an immediate exit. I trust the exact behavior is hidden somewhere in the documentation. :)

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                      G 1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        Hi George, my example was on Application.Exit() which causes an Application.Run() to return, I guess asynchronously by sending a Windows message or something; so the thread has the opportunity to first execute the finally block. Your test was on Environment.Exit() which, from your observation, seems to cause an immediate exit. I trust the exact behavior is hidden somewhere in the documentation. :)

                                        Luc Pattyn [Forum Guidelines] [My Articles]


                                        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                        G Offline
                                        G Offline
                                        George_George
                                        wrote on last edited by
                                        #19

                                        Thanks Luc, I think for console and Windows Service application, we should never use Application.Exit, right? regards, George

                                        L 1 Reply Last reply
                                        0
                                        • G George_George

                                          Thanks Luc, I think for console and Windows Service application, we should never use Application.Exit, right? regards, George

                                          L Offline
                                          L Offline
                                          Luc Pattyn
                                          wrote on last edited by
                                          #20

                                          Application.Exit() causes Application.Run() to return, both make sense in windows apps only. this has been asked and answered many times in the last couple of days, it is not going to change any time soon ... :)

                                          Luc Pattyn [Forum Guidelines] [My Articles]


                                          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                          G 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups