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. Exiting Windows Application (from the main form constructor)

Exiting Windows Application (from the main form constructor)

Scheduled Pinned Locked Moved C#
question
13 Posts 5 Posters 0 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.
  • D Dirso

    Hi, I have a Windows based application and I'd like to make some validations before running (like license or something) My question is if I do those validations inside the application main form and it's not successfuly done, I'd like to quit, I'd would prefer a friendly "Not Registered Application" message and then close it than throwing an exception.

    static class Program
    {
        /// /// The main entry point for the application.
        /// 
        \[STAThread\]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmGuardian());
        }
    }
    

    /// the main form constructor....
    public FrmGuardian()
    {
    if (!validInstall())
    this.Close(); // here I tried Application.Exit and Application.ExitThread

            using (FrmSplash splash = new FrmSplash())
            {
                splash.Show();
                InitializeComponent();
                splash.Close();
            }
        }
    

    In the current code I get

    Cannot access a disposed object.
    Object name: 'FrmGuardian'.

    Thanks, Dirso

    N Offline
    N Offline
    Nicholas Butler
    wrote on last edited by
    #4

    Hi Dirso, You can put the validation code in your Main method, before you even instantiate your form. Nick

    ---------------------------------- Be excellent to each other :)

    D 1 Reply Last reply
    0
    • D Dirso

      Hi, I have a Windows based application and I'd like to make some validations before running (like license or something) My question is if I do those validations inside the application main form and it's not successfuly done, I'd like to quit, I'd would prefer a friendly "Not Registered Application" message and then close it than throwing an exception.

      static class Program
      {
          /// /// The main entry point for the application.
          /// 
          \[STAThread\]
          static void Main()
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new FrmGuardian());
          }
      }
      

      /// the main form constructor....
      public FrmGuardian()
      {
      if (!validInstall())
      this.Close(); // here I tried Application.Exit and Application.ExitThread

              using (FrmSplash splash = new FrmSplash())
              {
                  splash.Show();
                  InitializeComponent();
                  splash.Close();
              }
          }
      

      In the current code I get

      Cannot access a disposed object.
      Object name: 'FrmGuardian'.

      Thanks, Dirso

      J Offline
      J Offline
      J a a n s
      wrote on last edited by
      #5

      Dirso wrote:

      public FrmGuardian() { if (!validInstall()) this.Close(); // here I tried Application.Exit and Application.ExitThread using (FrmSplash splash = new FrmSplash())

      Exit from the C'tor before executing the remaining code.. if (!validInstall()) {     this.Close();     return; }

      *jaans

      D 1 Reply Last reply
      0
      • N Nicholas Butler

        Hi Dirso, You can put the validation code in your Main method, before you even instantiate your form. Nick

        ---------------------------------- Be excellent to each other :)

        D Offline
        D Offline
        Dirso
        wrote on last edited by
        #6

        That would be the best solution, but I'd like some general solution that would work for Console Applications too. for example, what If one of my funcional classes can't load because its configuration is missing or if some device is offline... First, I ask if the user wants to try again, but if it quits, I need to shut it down. Thanks, Dirso

        N 1 Reply Last reply
        0
        • J J a a n s

          Dirso wrote:

          public FrmGuardian() { if (!validInstall()) this.Close(); // here I tried Application.Exit and Application.ExitThread using (FrmSplash splash = new FrmSplash())

          Exit from the C'tor before executing the remaining code.. if (!validInstall()) {     this.Close();     return; }

          *jaans

          D Offline
          D Offline
          Dirso
          wrote on last edited by
          #7

          Same exception

          L 1 Reply Last reply
          0
          • D Dirso

            Same exception

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            As we already told you, you can do this in the Load event handler of your form.

            D 1 Reply Last reply
            0
            • D Dirso

              Hi, I have a Windows based application and I'd like to make some validations before running (like license or something) My question is if I do those validations inside the application main form and it's not successfuly done, I'd like to quit, I'd would prefer a friendly "Not Registered Application" message and then close it than throwing an exception.

              static class Program
              {
                  /// /// The main entry point for the application.
                  /// 
                  \[STAThread\]
                  static void Main()
                  {
                      Application.EnableVisualStyles();
                      Application.SetCompatibleTextRenderingDefault(false);
                      Application.Run(new FrmGuardian());
                  }
              }
              

              /// the main form constructor....
              public FrmGuardian()
              {
              if (!validInstall())
              this.Close(); // here I tried Application.Exit and Application.ExitThread

                      using (FrmSplash splash = new FrmSplash())
                      {
                          splash.Show();
                          InitializeComponent();
                          splash.Close();
                      }
                  }
              

              In the current code I get

              Cannot access a disposed object.
              Object name: 'FrmGuardian'.

              Thanks, Dirso

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #9

              You should do all of your form initialization in the FormLoad method, and call Application.Exit() from that method if needed.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              1 Reply Last reply
              0
              • L Lost User

                As we already told you, you can do this in the Load event handler of your form.

                D Offline
                D Offline
                Dirso
                wrote on last edited by
                #10

                Yes, and it is a good idea. But I'd like something more general that could work on Console Applications too. Thanks, Dirso

                L 1 Reply Last reply
                0
                • D Dirso

                  That would be the best solution, but I'd like some general solution that would work for Console Applications too. for example, what If one of my funcional classes can't load because its configuration is missing or if some device is offline... First, I ask if the user wants to try again, but if it quits, I need to shut it down. Thanks, Dirso

                  N Offline
                  N Offline
                  Nicholas Butler
                  wrote on last edited by
                  #11

                  Console apps have a Main method too :confused: A console app exits when you return from Main. You could also add a reference to System.Windows.Forms if you want to show a message box or even a form. That would kind of subvert the console format, though. Nick

                  ---------------------------------- Be excellent to each other :)

                  D 1 Reply Last reply
                  0
                  • N Nicholas Butler

                    Console apps have a Main method too :confused: A console app exits when you return from Main. You could also add a reference to System.Windows.Forms if you want to show a message box or even a form. That would kind of subvert the console format, though. Nick

                    ---------------------------------- Be excellent to each other :)

                    D Offline
                    D Offline
                    Dirso
                    wrote on last edited by
                    #12

                    Nick Butler wrote:

                    Console apps have a Main method too Confused A console app exits when you return from Main.

                    I know... and I haven't thinking about moving my code to other methods than the construtors... I'll give it a try thanks, Dirso

                    1 Reply Last reply
                    0
                    • D Dirso

                      Yes, and it is a good idea. But I'd like something more general that could work on Console Applications too. Thanks, Dirso

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      What about the Main method then? Console applications also have a Main method. regards

                      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