Application.Run does not work after Application.Exit in the same topic

The requirement in my work is to first show a screen saver that takes some data from the user, authenticates him and launches another form. So, I start the splash screen with Application.Run, and once, it has completed its action, it will call Application.Exit, and then it will call Application.Run again for the new form. But this does not launch a new form. I have a new thread and assign it to ApartmentModel as STA to run the new form.

I would like to understand why Application.Run does not work after calling Application.Exit?

Code example:

Application.Run(SplashForm); if (_authorizationSuccessful) Application.Run(new Form2()) else { //just close } 
+4
source share
5 answers

It is rather confusing, but the main problem is that this is actually not a form that supports the application. The key helper class is ApplicationContext, which also appears as one of Application.Run () overloads. When you use other overloads, Run () and Run (Form), the Application class creates a hidden instance of ApplicationContext. What is global, like all properties and methods of an application.

Therefore, when you call Application.Exit (), you mark this global instance of ApplicationContext as "more inactive." Which makes the next run (Form) call complete immediately.

Instead, you can monkey with your ApplicationContext instances to solve this problem. But the template solution should use the ShowDialog () method in the login form. Also a good way to return the state of "login is good, let continue", it is useful to use DialogResult.

+3
source

First you need to show the main form (the second form in your case) in this form, and you need to call up the splash screen (authentication) until the main form is hidden. In Splash Screen, you need to use this.Close () instead of Application.Exit (). After closing the splash screen, you need to display the main form.

0
source

Do it like

 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 for the current thread and displays the specified form. The message loop allows the form to receive Windows messages (for example, keystrokes, mouse clicks, paint invalidation) to allow it to be responsive and have user interaction

So, when you do Application.Exit() , the whole application closes, not the specific form

0
source

Instead of tearing everything up, just to create it again, why not do it:

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

Where Form1 is a splash screen. Then, when he is ready to show the main screen, he can do this:

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

Where Form2 is the main screen. The message loop (single) continues to run.

0
source

Throw an exception for the main method only when you want to exit the application, catch the exception and exit the application as necessary.

Let's pretend that

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

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

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

Source: https://habr.com/ru/post/1439059/


All Articles