What is the difference between Application.Run () and Form.ShowDialog ()?

In my application, I want to open the login form first, and then the main form, if the login was successful. I am currently doing it something like this:

var A = new LoginForm(); if ( A.ShowDialog() == DialogResult.OK ) Application.Run(new MainForm()); 

But then I started to wonder - what is the point of Application.Run() ? Why not just do (new MainForm()).ShowDialog() ? What's the difference? And what would be the right way to achieve what I want?

+66
c # winforms
Feb 22 '10 at 21:52
source share
8 answers

Application.Run(Form) starts a message loop in the current thread and displays the specified form. The message loop allows forms to receive Windows messages (for example, keystrokes, mouse clicks, invalidation) so that it can respond and interact with the user. When you call ShowDialog() on an instance of Form , it actually does the same thing and creates a modal message loop for the form on which ShowDialog was called.

There is not much difference between the two calls. Application.Run adds some additional event processing that allows you to do some cleanup of resources when closing the main form (see Application.ThreadExit ).

The recommended way to run WinForms applications is to use Application.Run , but I suspect this is more an agreement than a rule. The biggest reason to use Application.Run is if you want to open several modeless forms. You can do this using:

 new Form().Show(); new Form().Show(); Application.Run(); 

You could not achieve this using the ShowDialog() method, since one of the forms must be modal.




As for your question on how to show the login form, and then the main form, if the login is successful, I think that everything is fine with you:

 if (new LoginForm().ShowDialog() == DialogResult.OK) { Application.Run(new MainForm()); } 

An alternative is to do the sanning yourself and open the MainForm instance in the LoginForm close LoginForm , if the login was successful.

+61
Feb 22 '10 at 23:47
source share

From my testing, I noticed this main difference:

When Application.Run is used, the button to close the form (red X) returns DialogResult.None; however, when ShowDialog is used, the Close button calls DialogResult.Cancel.

This is for you? In my code, I tested DialogResult.Cancel to determine the exit code of my application. This was broken when red X was used to close the form. Now I am testing DialogResult.OK to indicate a successful exit.

  return myForm.DialogResult == DialogResult.OK ? 0 : 1; 
+5
Sep 15 '11 at 18:53
source share

From MSDN:

This method adds an event handler to the mainForm parameter for a private event. Calling an ExitThread event handler to clean up the application.

http://msdn.microsoft.com/en-us/library/ms157902.aspx

+4
Feb 22 '10 at 22:00
source share

One of the key differences is that ShowDialog is usually a modal dialog. If you want to create a convenient set of tools, you would not want it to consist of modal dialog boxes.

In addition, Application.Run () takes more than just form. It has several overloads.

As for your application, I don’t think it matters much. Application.Run makes sense to me because it means the start of your actual Application.

+2
Feb 22 '10 at 22:00
source share

transhipment documentation

 public static void Run( ApplicationContext context ); 

has a neat example with a different approach that also includes two forms.

+2
May 30 '12 at 10:00
source share

For a more specific example of the difference:

If your main form is an MDI form, the behavior when you click the close button ("x" in the upper right corner or Alt-F4) differs depending on which method you use to display the form.

Application.Run(mainForm) fires the event of closing child forms, then fires the event of closing the main form.

With mainForm.ShowDialog , the closing event of the main form is mainForm.ShowDialog , and the closing event of child forms is not executed.

+2
Jan 10 '13 at 8:53
source share

Application.Run() intended to start the application, and MainForm is part of the application, and MainForm()).ShowDialog() used only to display it.

Application.Run() is the entry point for your application. same as Main() method for some class or ApplicationStart() for WebApplication

Application.Run () has different overloads, one of which has no parameters. This method starts the application without an initial form.

+1
Feb 22 2018-10-22
source share

From my testing, I notice that using Application.Run buttons with DialogResult does not close the form (OnFormClosing does not fall) compares to ShowDialog, in which the buttons with DialogResult hit OnFormClosing and close the form.

0
Nov 01 '13 at 15:30
source share



All Articles