Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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?

like image 413
Vilx- Avatar asked Feb 22 '10 21:02

Vilx-


People also ask

What is the difference between form show () and form ShowDialog ()?

Show() method shows a windows form in a non-modal state. ShowDialog() method shows a window in a modal state and stops execution of the calling context until a result is returned from the windows form open by the method.

What is ShowDialog?

ShowDialog shows the window, disables all other windows in the application, and returns only when the window is closed. This type of window is known as a modal window. Modal windows are primarily used as dialog boxes.

What is application run in C#?

Run() Begins running a standard application message loop on the current thread, without a form. Run(ApplicationContext) Begins running a standard application message loop on the current thread, with an ApplicationContext.

What is ShowDialog C#?

ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.


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

like image 154
adrianbanks Avatar answered Oct 06 '22 01:10

adrianbanks


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

like image 33
BFree Avatar answered Oct 05 '22 23:10

BFree