Application.Exit(); not working in the form constructor, why?
-
Hello, I'm using Application.Exit(); in the form contructor to terminate the application if certain condition is met... but it's not working! Why? and what can I do? Thanks...
Hello I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right? Well if you take a look ate Program.cs file where the main method exists, you'd see this line
Application.Run(new MyForm());
So actually it excutes more like this:
MyForm temp = new MyForm()
Application.Run(temp)
//Then temp dies hereWell, not exactly but I made it this way to illustrate that your constructor must return first before the
Run()
method actually gets called, and of course calling theExit()
method is useless if theRun()
didn't get excuted first. Suggested solution: Simple; Make theApplication.Exit()
call in your Form_Load event if applicable.Regards:rose:
-
Hello I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right? Well if you take a look ate Program.cs file where the main method exists, you'd see this line
Application.Run(new MyForm());
So actually it excutes more like this:
MyForm temp = new MyForm()
Application.Run(temp)
//Then temp dies hereWell, not exactly but I made it this way to illustrate that your constructor must return first before the
Run()
method actually gets called, and of course calling theExit()
method is useless if theRun()
didn't get excuted first. Suggested solution: Simple; Make theApplication.Exit()
call in your Form_Load event if applicable.Regards:rose:
-
Hello I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right? Well if you take a look ate Program.cs file where the main method exists, you'd see this line
Application.Run(new MyForm());
So actually it excutes more like this:
MyForm temp = new MyForm()
Application.Run(temp)
//Then temp dies hereWell, not exactly but I made it this way to illustrate that your constructor must return first before the
Run()
method actually gets called, and of course calling theExit()
method is useless if theRun()
didn't get excuted first. Suggested solution: Simple; Make theApplication.Exit()
call in your Form_Load event if applicable.Regards:rose: