tags:

views:

56

answers:

3

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 its fully loaded?

Just to make it clear I want to prevent application from loading in case of database connection issue.

+2  A: 

Try Environment.Exit as described here.

ho1
+2  A: 

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.

Kevin van Zanten
A: 

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.

Ben Cawley
As I said below the Question it is probably the best solution to the general problem, that I'm going to develop.
Cornelius