Exiting Windows Application (from the main form constructor)
-
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.ExitThreadusing (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
Hi Dirso, You can put the validation code in your
Main
method, before you even instantiate your form. Nick---------------------------------- Be excellent to each other :)
-
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.ExitThreadusing (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
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
-
Hi Dirso, You can put the validation code in your
Main
method, before you even instantiate your form. Nick---------------------------------- Be excellent to each other :)
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
-
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
-
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.ExitThreadusing (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
You should do all of your form initialization in the
FormLoad
method, and callApplication.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 -
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
Console apps have a
Main
method too :confused: A console app exits when you return fromMain
. You could also add a reference toSystem.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 :)
-
Console apps have a
Main
method too :confused: A console app exits when you return fromMain
. You could also add a reference toSystem.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 :)
-
Yes, and it is a good idea. But I'd like something more general that could work on Console Applications too. Thanks, Dirso