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.
adrianbanks Feb 22 '10 at 23:47 2010-02-22 23:47
source share