Is it possible to prevent MainForm from loading fully during the process of starting up an application (not sure how its called, Component Initialization maybe)?
I've tried:
public MainForm()
{
if (true)
{
Application.Exit();
return;
}
InitializeComponent();
}
and
public MainForm()
{
if (true)
{
this.Close();
Application.Exit();
return;
}
InitializeComponent();
}
and without "return;" as well.
The first one does actually nothing, while the second solution throws up an "Cannot access a disposed object." error?
Is it even possible to close whole Application before开发者_Go百科 its fully loaded?
Just to make it clear I want to prevent application from loading in case of database connection issue.
As ho1 said, Environment.Exit
is the answer. For example:
public MainForm()
{
if (true)
{
Environment.Exit(0);
}
InitializeComponent();
}
That will cause the application to close if the condition is true
in the if-statement.
Try Environment.Exit
as described here.
I think the answer given by rob_g is the way to go. Having the database initialized and validated prior to showing the form is the neatest solution in my opinion! You also remove unnecessary logic from the form constructor, as the form shouldnt really care about db initialization.
精彩评论