Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close application before its fully loaded?

Tags:

c#

.net

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.

like image 523
Marek Avatar asked Jun 21 '10 09:06

Marek


2 Answers

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.

like image 187
Fusyion Avatar answered Nov 12 '22 17:11

Fusyion


Try Environment.Exit as described here.

like image 8
Hans Olsson Avatar answered Nov 12 '22 15:11

Hans Olsson