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. Exit a WinForm application at initialization

Exit a WinForm application at initialization

Scheduled Pinned Locked Moved C#
windows-adminquestion
8 Posts 3 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.
  • G Offline
    G Offline
    Glen Harvy 0
    wrote on last edited by
    #1

    Hi, I have a WinForm that needs to run only if a registry entry exists. If not, I want the application to immediately exit. I don't believe you can call Application.Exit until after initialization of the form is complete. Is there a workaround or better solution to achieve my needs?

    Glen Harvy

    S 1 Reply Last reply
    0
    • G Glen Harvy 0

      Hi, I have a WinForm that needs to run only if a registry entry exists. If not, I want the application to immediately exit. I don't believe you can call Application.Exit until after initialization of the form is complete. Is there a workaround or better solution to achieve my needs?

      Glen Harvy

      S Offline
      S Offline
      Syed Muhammad Kamran
      wrote on last edited by
      #2

      Why not do it in Form's Constructor.... It's run b4 Initialization. You can call dispose from there. Or even better you can use a static constructor try to set a static boolean variable and check it's value b4 instantiating... Any Static Construct is the first thing which is run After class loading.

      G 1 Reply Last reply
      0
      • S Syed Muhammad Kamran

        Why not do it in Form's Constructor.... It's run b4 Initialization. You can call dispose from there. Or even better you can use a static constructor try to set a static boolean variable and check it's value b4 instantiating... Any Static Construct is the first thing which is run After class loading.

        G Offline
        G Offline
        Glen Harvy 0
        wrote on last edited by
        #3

        Syed Muhammad Kamran wrote:

        Or even better you can use a static constructor try to set a static boolean variable and check it's value b4 instantiating... Any Static Construct is the first thing which is run After class loading.

        Thanks for your assistance ... I have a rough idea of what you suggest but would appreciate your elaborating a bit. This is what I have now: namespace blahblah { public partial class MainForm : Form { public MainForm() { bool startupCheckValue = true; startupCheckValue = startupCheck(); if (!startupCheckValue) { Dispose(); this.Close(); Application.Exit(); // exitProgram(); } etc etc etc private bool startupCheck() { // make sure database file exists // [code works fine] } How do I go about implementing what you suggest. Thanks in advance.

        Glen Harvy

        S 1 Reply Last reply
        0
        • G Glen Harvy 0

          Syed Muhammad Kamran wrote:

          Or even better you can use a static constructor try to set a static boolean variable and check it's value b4 instantiating... Any Static Construct is the first thing which is run After class loading.

          Thanks for your assistance ... I have a rough idea of what you suggest but would appreciate your elaborating a bit. This is what I have now: namespace blahblah { public partial class MainForm : Form { public MainForm() { bool startupCheckValue = true; startupCheckValue = startupCheck(); if (!startupCheckValue) { Dispose(); this.Close(); Application.Exit(); // exitProgram(); } etc etc etc private bool startupCheck() { // make sure database file exists // [code works fine] } How do I go about implementing what you suggest. Thanks in advance.

          Glen Harvy

          S Offline
          S Offline
          Syed Muhammad Kamran
          wrote on last edited by
          #4

          namespace blahblah { public partial class MainForm : Form { static bool startUpCheckValue = false; static MainForm (){ startupCheckValue = StartupCheck(); } public MainForm() { if (!startupCheckValue) { Dispose(); this.Close(); Application.Exit(); // exitProgram(); } etc etc etc private static bool startupCheck() { // make sure database file exists // [code works fine] }

          G 1 Reply Last reply
          0
          • S Syed Muhammad Kamran

            namespace blahblah { public partial class MainForm : Form { static bool startUpCheckValue = false; static MainForm (){ startupCheckValue = StartupCheck(); } public MainForm() { if (!startupCheckValue) { Dispose(); this.Close(); Application.Exit(); // exitProgram(); } etc etc etc private static bool startupCheck() { // make sure database file exists // [code works fine] }

            G Offline
            G Offline
            Glen Harvy 0
            wrote on last edited by
            #5

            Thanks for that - I have since done some research and certainly understand constructors and specifically static constructors and methods a little better :) Frankly, I agree your suggestion should work - at least in theory. Unfortunately I still can't get the program to exit. I made the changes you suggested without success and then as an extreme test changed it to the following: public partial class MainForm : Form { static MainForm() { Application.Exit(); } public MainForm() { [etc etc] I'll keep delving into this and welcome any other suggestions.

            Glen Harvy

            S 1 Reply Last reply
            0
            • G Glen Harvy 0

              Thanks for that - I have since done some research and certainly understand constructors and specifically static constructors and methods a little better :) Frankly, I agree your suggestion should work - at least in theory. Unfortunately I still can't get the program to exit. I made the changes you suggested without success and then as an extreme test changed it to the following: public partial class MainForm : Form { static MainForm() { Application.Exit(); } public MainForm() { [etc etc] I'll keep delving into this and welcome any other suggestions.

              Glen Harvy

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              Application.Exit will nit work inside the constructor, because Application.Run wasn't executed yet. If you look at the static Main method of your application it looks similar to this:

              static void Main()
              {
              Applivation.Run(new MainForm());
              }

              So what happens, is that an instance of the MainForm class is created (calling your constructor) and afterwards passed to the Application.Run method. That's why Application.Exit does not work inside the constructor. It would probably be the best to assign the instance of MainForm to a variable and only call Application.Run, if your MainForm could be properly initialized:

              static void Main()
              {
              MainForm form = new MainForm();
              if (something)
              {
              Applivation.Run(form);
              }
              else
              {
              Show some kind of error message
              }
              }


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              G 1 Reply Last reply
              0
              • S Stefan Troschuetz

                Application.Exit will nit work inside the constructor, because Application.Run wasn't executed yet. If you look at the static Main method of your application it looks similar to this:

                static void Main()
                {
                Applivation.Run(new MainForm());
                }

                So what happens, is that an instance of the MainForm class is created (calling your constructor) and afterwards passed to the Application.Run method. That's why Application.Exit does not work inside the constructor. It would probably be the best to assign the instance of MainForm to a variable and only call Application.Run, if your MainForm could be properly initialized:

                static void Main()
                {
                MainForm form = new MainForm();
                if (something)
                {
                Applivation.Run(form);
                }
                else
                {
                Show some kind of error message
                }
                }


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                G Offline
                G Offline
                Glen Harvy 0
                wrote on last edited by
                #7

                Thanks very much - I've learn't heaps tonight.:-D

                Glen Harvy

                S 1 Reply Last reply
                0
                • G Glen Harvy 0

                  Thanks very much - I've learn't heaps tonight.:-D

                  Glen Harvy

                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #8

                  My pleasure :)


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  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