Exit a WinForm application at initialization
-
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
-
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
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.
-
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.
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
-
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
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] }
-
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] }
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
-
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
Application.Exit
will nit work inside the constructor, becauseApplication.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
-
Application.Exit
will nit work inside the constructor, becauseApplication.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
Thanks very much - I've learn't heaps tonight.:-D
Glen Harvy
-
Thanks very much - I've learn't heaps tonight.:-D
Glen Harvy
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