right after InitializeComponent(), how do I close the application?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • titan.nyquist@gmail.com

    right after InitializeComponent(), how do I close the application?

    Right after InitializeCompo nent() is run, I do some error checking...
    when an (very bad) error occurs I show it to the user and I want to
    close down the application.

    QUESTION: How do I close the application down? I am assuming I can
    do it nearly right after InitializeCompo nent() is run, in the same
    code location it exists in.

    I'm new, so any help is great. Any articles on the topic would help,
    too.

    Titan

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: right after InitializeCompo nent(), how do I close the application?

    Titan,

    You can just call the static Exit method on the Application class. This
    assumes that this is a windows forms app. If it is a console app, then you
    will want to call the Exit method on the Environment class.

    It should be noted that you should not leave it to your components to
    perform this operation. It screams of poor design. If anything, the
    hosting executable should be informed in some way (through an exception, a
    return value, something) that an error has occured, and then you can just
    have the host application exit itself.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <titan.nyquist@ gmail.comwrote in message
    news:1175198303 .047620.275290@ o5g2000hsb.goog legroups.com...
    Right after InitializeCompo nent() is run, I do some error checking...
    when an (very bad) error occurs I show it to the user and I want to
    close down the application.
    >
    QUESTION: How do I close the application down? I am assuming I can
    do it nearly right after InitializeCompo nent() is run, in the same
    code location it exists in.
    >
    I'm new, so any help is great. Any articles on the topic would help,
    too.
    >
    Titan
    >

    Comment

    • titan.nyquist@gmail.com

      #3
      Re: right after InitializeCompo nent(), how do I close the application?

      Nicholas,

      That does not seem to work.

      I just created the most simple WIndows application, and added a call
      to Application.Exi t() immediately after InitializeCompo nent(), and it
      seems to do nothing. The application stays loaded until the user
      clicks to close it. The code:

      using System;
      using System.Collecti ons.Generic;
      using System.Componen tModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows. Forms;

      namespace ShutDownOnImmed iateFatalErrorT est
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeCompo nent();
      Application.Exi t(); // <---- this is the only line of code i added
      }
      }
      }

      Comment

      • titan.nyquist@gmail.com

        #4
        Re: right after InitializeCompo nent(), how do I close the application?

        If I try to run Close(), like so:

        using System;
        using System.Collecti ons.Generic;
        using System.Componen tModel;
        using System.Data;
        using System.Drawing;
        using System.Text;
        using System.Windows. Forms;

        namespace ShutDownOnImmed iateFatalErrorT est
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeCompo nent();
        Close(); // <---- here's where I call Close()
        }
        }
        }

        This will close down Form1, but too prematurely. Then
        Application.Run (new Form1()) in Main() has lost a resource it uses,
        and cannot access the disposed object. So, although this works
        somewhat, it crashes the program.

        I'm so new at this, can you give me any insight on how to gracefully
        close down the application?

        Titan

        Comment

        • Marc Gravell

          #5
          Re: right after InitializeCompo nent(), how do I close the application?

          seems an odd place to want to exit... however, you could perhaps throw
          an exception (caught from your code), or set a property to say "I'm
          invalid".

          At last ditch, you could look at Process.GetCurr entProcess().Ki ll() or
          Environment.Fai lFast(), but neither of these are very graceful...

          Marc


          Comment

          • titan.nyquist@gmail.com

            #6
            Re: right after InitializeCompo nent(), how do I close the application?

            Apparantly, Application.Exi t() does NOTHING inside the main form
            (Form1 in my code above) constructor.

            Dave Sexton, in this thread: http://www.thescripts.com/forum/thread592698.html,
            says the error checking should be handled BEFORE the main form is even
            created. This makes sense, especially if all you are going to do is
            shut down the main form, the one you just created for nothing if a
            fatal error occurs.

            I'll try it and post back...

            Titan

            Comment

            • titan.nyquist@gmail.com

              #7
              Re: right after InitializeCompo nent(), how do I close the application?

              Dave Sexton seems to have the answer. This works (this code from the
              default Windows application, same as the code above only this time it
              is the Program.cs file):


              using System;
              using System.Collecti ons.Generic;
              using System.Windows. Forms;

              namespace ShutDownOnImmed iateFatalErrorT est
              {
              static class Program
              {
              /// <summary>
              /// The main entry point for the application.
              /// </summary>
              [STAThread]
              static void Main()
              {
              Application.Ena bleVisualStyles ();
              Application.Set CompatibleTextR enderingDefault (false);

              bool fatalError = true;
              if (fatalError == false)
              {
              Application.Run (new Form1()); // <--- enclosed Application.Run ()
              inside an error checking if statement
              // it never runs if a fatal error occurs, and therefore no need
              to close it down
              }
              }
              }
              }


              (Note: I did not change the Form1.cs file.)

              Titan

              Comment

              Working...