Pradeep Singh
Pradeep Singh

Reputation: 243

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

In the following piece of code I have a watcher that look if file has changed and if it has changed I show the changed information on a form but if i use form.Show() for this it freezes but form.showDialog() works fine, what is the difference between these two and how to determine which one to use

    private void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        _watcher.EnableRaisingEvents = false;
        try
        {
            if (_displayPatientInfo != null)
            {
                _displayPatientInfo.Dispose();
            }
            GetPatientInfo(e.FullPath);
            using (StreamReader sr = new StreamReader(e.FullPath, Encoding.Default))
            {
                String line;
                line = sr.ReadToEnd();

                if (line.IndexOf("<IsPatientFixed>") > 0)
                {
                    var value = GetTagValue(line, "<IsPatientFixed>", "</IsPatientFixed>");
                    if (value == "true" || value == "True")
                    {

                        _displayPatientInfo = new frmPatientInfoDisplay();
                        _displayPatientInfo.SetData(_patientInfo);
                        _displayPatientInfo.ShowDialog();

                    }
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            _watcher.EnableRaisingEvents = true;
        }
    }

Upvotes: 0

Views: 6433

Answers (2)

Mihai Hantea
Mihai Hantea

Reputation: 1743

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.

Upvotes: 4

Habib
Habib

Reputation: 223257

ShowDialog halts the program flow, until you close the form, whereas Show displays the form and continues with the program flow.

Form.ShowDialog - MSDN

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

The problem is because of the using block. ShowDialog method is blocking the program flow until the form is closed, because of that using block will not terminate. Show on the other hand returns control to the next line immediately and since you created the form object inside the using block, it will not be visible outside of the block. That is why you get your form stuck.

Upvotes: 2

Related Questions