Home » c# » What's the difference between Application.Run() and Form.ShowDialog()?

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

Posted by: admin November 29, 2017 Leave a comment

Questions:

In my application I want to show a login form first and then the main form if the login has been successful. Currently I’m doing it something like this:

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

But then I started wondering – what’s the point of the Application.Run()? Why not just do (new MainForm()).ShowDialog() as well? What’s the difference? And what would be the correct way to achieve what I want?

Answers:

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. When you call ShowDialog() on a Form instance, it actually does a similar thing and creates a modal message loop for the form on which ShowDialog has been called.

There is not much difference between the two calls. Application.Run does add some extra event handling enabling you to do some tidying up of resources when the main form is closed (see Application.ThreadExit).

The recommended way to start WinForms applications is using Application.Run, but I suspect this is more of a convention than a rule. The biggest reason to use Application.Run is if you want to open multiple non-modal forms. You can do this using:

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

You could not achieve this using the ShowDialog() method as one of the forms would have to be modal.


As for your question of how to show a login form and then the main form if the login is successful, I think what you have is fine:

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

The alternative is to do the plumbing yourself and open an instance of MainForm in the closing event of the LoginForm if the login was successful.

Questions:
Answers:

From my testing, I noticed this main difference:

When Application.Run is used, the form’s Close button (red X) returns DialogResult.None; however, when ShowDialog is used, the Close button produces DialogResult.Cancel.

Does this matter to you? In my code, I was testing for DialogResult.Cancel to determine the exit code of my application. That was broken when the red X was used to close the form. I now test for DialogResult.OK to indicate a successful exit.

        return myForm.DialogResult == DialogResult.OK ? 0 : 1;

Questions:
Answers:

From MSDN:

This method adds an event handler to
the mainForm parameter for the Closed
event. The event handler calls
ExitThread to clean up the
application.

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

Questions:
Answers:

One key difference is that ShowDialog is usually a modal Dialog. If you wanted to create a user-friendly toolset, you would not want it to be comprised of modal dialog boxes.

Also, Application.Run() accepts more than just a form. It has a few overloads.

As for your application, I do not think it matters much. Application.Run makes sense to me because it denotes the start of your actual Application.

Questions:
Answers:

The documentation of the overload

public static void Run(
    ApplicationContext context );

has a neat example with a different approach that involves two forms as well.

Questions:
Answers:

For a more concerete example of a difference:

If your main form is an MDI form, then the behavior on clicking the close button (the ‘x’ in the upper right, or Alt-F4) is different depending on which method you use to show the form.

With Application.Run(mainForm), the closing event of the child forms run, then the main form’s closing event runs.

With mainForm.ShowDialog, the closing event of the main form runs, and the closing event of the child forms do not run.

Questions:
Answers:

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

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

Application.Run() has different overloads, one of which is without parameters. That Method starts application without an initial form.

Questions:
Answers:

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