What's the Difference Between Application.Run() and Form.Showdialog()

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

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.

What's the difference between Show(), ShowDialog() and Application.Run() functions?

The Show function shows the form in a non modal form. This means that you can click on the parent form.

ShowDialog shows the form modally, meaning you cannot go to the parent form

Application.Run() runs the main parent form, and makes that form the main form. Application.Run() is usually found in main.

Form.ShowDialog() or Form.ShowDialog(this)?

Just to better understand the owner-owned relationship:

.NET allows a form to “own” other forms. Owned forms are useful for
floating toolbox and command windows. One example of an owned form is
the Find and Replace window in Microsoft Word. When an owner window is
minimized, the owned forms are also minimized automatically. When an
owned form overlaps its owner, it is always displayed on top.

(c) "Pro .NET 2.0 Windows Forms and Custom Controls" by Matthew MacDonald.


As ShowDialog shows the new form, an implicit relationship is
established
between the currently active form, known as the owner
form, and the new form, known as the owned form. This relationship
ensures that the owned form is the active form and is always shown on
top of the owner form.

One feature of this relationship is that the owned form affects the
behavior of its owner form (when using ShowDialog):

  • The owner form cannot be minimized, maximized, or even moved.
  • The owned form blocks mouse and keyboard input to the owner form.
  • The owner form is minimized when the owned form is.
  • Only the owned form can be closed.
  • If both owner and owned forms are minimized and if the user presses Alt+Tab to switch to the owned form, the owned form is activated.

Unlike the ShowDialog method, however, a call to the Show method does
not establish an implicit owner-owned relationship
. This means that
either form can be the currently active form.

Without an implicit owner-owned relationship, owner and owned forms
alike can be minimized, maximized, or moved. If the user closes any
form other than the main form, the most recently active form is
reactivated.

Although ShowDialog establishes an implicit owner-owned relationship,
there is no built-in way for the owned form to call back to or query
the form that opened it. In the modeless case, you can set the new
form's Owner property to establish the owner-owned relationship. As a
shortcut, you could pass the owner form as an argument to an overload
of the Show method, which also takes an IWin32Window parameter
(IWin32Window is implemented by Windows Forms UI objects that expose a
Win32 HWND property via the IWin32Window.Handle property).

The behavior of forms in an explicit modal owner-owned form
relationship is the same as its implicit modal counterpart, but the
modeless owner-owned relationship provides additional behavior in the
non-owner-owned modeless case. First, the modeless owned form always
appears on top of the owner form, even though either can be active.
This is useful when you need to keep a form, such as a floating tool
window, on top of other forms within an application. Second, if the
user presses Alt+Tab to switch from the owner, the owned forms follow
suit. To ensure that the user knows which form is the main form,
minimizing the owner hides the task bar buttons for all owned forms,
leaving only the owner's task bar button visible.

(c) "Windows Forms 2.0 Programming" by Chris Sells, Michael Weinhardt.

why does form.showdialog() works and form.show() doesn't in the following code

As @Habib said when you call ShowDialog() the code after this is not executed until you close the form and your watcher will be stuck.

Your problem is that the watcher is running on a different thread then your main form, that's why when you call Show() it will freeze your application because it's trying to access a portion of memory that is owned by your main thread.
To fix this you can use Invoke(Delegate) when you want to show or dispose _displayPatientInfo form.

Control.Invoke Method (Delegate)

    Executes the specified delegate on the thread that owns the control's underlying window handle.

Application.run(Windows) vs Application.run()

both are basically meant for message loop, it is the core of windows application which handles the window message like painting, mouse/kbd event etc.

if you use the code below without Application.Run

Window a = new Window ();
a.Show ();

you'll find a frozen window, the reason is that there is no one to tell that window to repaint or handle any event.

so by invoking a message loop via Application.Run, the window starts to work as expected

Application b = new Application ();
b.Run (a); // with a

Open form as form.ShowDialog() and proceed wih function in parent form

A sample "Please Wait" form with.
Here there is two form
1. Main Form with a Button
2. Form Designed to show as "Please wait" message. The Form contains two control
a. Label
b. Progress Bar - the property style for the progress bar is made as Marquee

Sample Image

  1. PleaseWait.cs

    public partial class PleaseWait : Form
    {
    public Action Worker { get; set; }
    public PleaseWait(Action worker)
    {
    InitializeComponent();

    if(worker == null)
    throw new ArgumentNullException();

    Worker = worker;
    }

    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    Task.Factory.StartNew(Worker).ContinueWith(a=>
    {
    this.Close();
    }, TaskScheduler.FromCurrentSynchronizationContext());
    }
    }
  2. MainForm.cs . In button1_click you will initialize the PleaseWait Form as ShowDialog

    private void button1_Click(object sender, EventArgs e)
    {
    using (PleaseWait pw = new PleaseWait(MakeAzureSQLQuery))
    {
    pw.ShowDialog();
    }
    }

    private void MakeAzureSQLQuery()
    {
    //Making the Query Function
    //You can also use Background Thread for Querying
    for (int nCount = 0; nCount < 500; nCount++)
    Thread.Sleep(50);
    }

Windows form application sequential ShowDialog()s

in second form formClosing() event i wrote these codes:

private void settingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason != CloseReason.UserClosing)
{
e.Cancel = true;
}
}

and nothing can close the second form except user!



Related Topics



Leave a reply



Submit