Question

The requirement in my work is to first show a splash screen which takes in a some data from user, authenticates him and starts another form. So, I am starting the splash screen with Application.Run and once, it has done its done, call Application.Exit and then call Application.Run again for the new form. But, this doesn't start the new form. I have a create a new thread and assign its ApartmentModel as STA to start the new form.

I would like to understand why is Application.Run not working after calling Application.Exit?

Sample Code:

        Application.Run(SplashForm);  

        if (_authorizationSuccessful)
            Application.Run(new Form2())
        else
        {
            //just close
        }
Was it helpful?

Solution

It is fairly convoluted, but the core issue that it isn't actually a Form that keeps an Application alive. The core helper class is ApplicationContext, also exposed as one of the overloads of Application.Run(). When you use the other overloads, Run() and Run(Form), the Application class creates a hidden instance of ApplicationContext. Which is global, like all of the Application properties and methods.

So when you call Application.Exit(), you mark that global instance of ApplicationContext as "no longer active". Which makes the next call to Run(Form) immediately exit.

You can monkey with your own ApplicationContext instances instead to solve this issue. But the boilerplate solution is to use the ShowDialog() method on the login form. Also a good way to return the "login is good, let's continue" state, the DialogResult is useful.

OTHER TIPS

First you need to show the main form(Second Form in your case) in this form u need to call the Splash Screen(Authentication) until that time main form will be hided. In Splash Screen you need to use this.Close() instead of Application.Exit(). After closing the splash screen u need to show the main form.

Do like this

new SplashScreenForm().ShowDialog();//show splash screen and take user input
new MainForm().Show();//now show the main form
Application.Run();//starts the app

OR

if (new SplashScreenForm().ShowDialog() == DialogResult.OK)//if its ok to start another form
{
    Application.Run(new MainForm());
}

NOTE

Application.Run(Form) starts a message loop on the current thread and displays the specified form. The message loop enables the form to receive Windows messages (eg, key presses, mouse clicks, paint invalidations) to allow it to appear responsive and have interaction with the user

So,when you do Application.Exit() the whole app is closed down,not the particular form

Rather than tearing everything down, just to build it up again, why not do this:

var f1 = new Form1();
f1.Show();
Application.Run();

Where Form1 is the splash screen. Then when it's ready to show the main screen, it can do this:

var f2 = new Form2();
f2.Show();
this.Close();

Where Form2 is the main screen. The (single) message loop stays running throughout.

Throw an exception to main method just when you want to exit your application, catch the exception and exit the app as required.

Suppose,

try
{
    // your code here
}
catch
{
    // Some exception occurred here, and you want to exit here...
    throw;
}

Catch the exception in your main program, and main program code be:

try
{
    Application.Run(new Form1());// <-- This is where you want to run your app
}
catch // <-- Catch here the exception where you want to exit your app
{
    Application.Exit(); //<-- exit app here.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top