4

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
        }
Sheogorath
  • 43
  • 5
  • Could you post the offending code? We can't help you in a problem like this without some idea of the steps you took programmatically to obtain your error. – jrd1 Oct 11 '12 at 03:43
  • You might want `ExitThread()` instead of `Application.Exit`? – Bobson Oct 11 '12 at 03:59
  • When you call Application.Exit the application exits. The next line of code will never be executed as the program has already stopped running. – BoltBait Oct 11 '12 at 05:00

5 Answers5

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

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.

andy
  • 5,979
  • 2
  • 27
  • 49
0

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

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Application.Exit doesn't bring down the process. It only terminates all the existing message loops and forces Run to return. Why can't I call another Application.Run after it returns from the first? – Sheogorath Oct 11 '12 at 06:55
  • @user1736766 msdn as always is quite messy.They have no proper documentation about this.. – Anirudha Oct 11 '12 at 07:05
0

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.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • In my case, only an assembly which exposes an entry point i.e. Main is provided. So, I can't exactly do "f2.Show()". – Sheogorath Oct 11 '12 at 09:46
0

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.
}