C # Winform: Is Application.Run () useful if I run a dialog form?

Can I just do in Program.cs

Form1.ShowDialog();

      

And in Form1:

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }

      

That is, I never use Application.Run () like the default VStudio code?

Is there any annoyance to do this?

+2


source to share


4 answers


I always like to stick with the form class methods. Form.ShowDialog()

and Form.Close()

. Everything you need is included Form.DialogResult

. If you stick with just instantiating the forms and using the methods attached to them, this seems much cleaner in the long run. I don't feel like you will get anything out of using Application.Run () when launching the form dialog.



+1


source


Yes, it ShowDialog

will create and run its own message loop. In fact, there are many different methods that you would not even think of to do this.

Application.Exit

instructs all running message loops to exit. I do not know enough about your statement to make a recommendation or use this method. But, in basic scenarios, I think you will find that its usefulness is limited at best.



Definitely when you don't want to use the default code that VS generates in a relation Application.Run

, but your statement you never use concerns me.

+1


source


You can do this, but this is not a good design. Program.cs starts the application and contains skills, which means starting and stopping the application. The form it launches is a window that the user interacts with and should just contain this code.
For example, what if you later want to include this form in another application? Well, you have to change it because it calls Application.Exit (), which would be problematic. Also, if you have automated tests of a GUI module calling Application.Exit () is also not good.

0


source


You should just rely on the Form.Close () call to return control to the caller, not Application.Exit (). If the caller is Program.cs, then your application will terminate.

0


source







All Articles