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

前端 未结 2 685
旧巷少年郎
旧巷少年郎 2021-01-28 21:22

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

2条回答
  •  离开以前
    2021-01-28 22:06

    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.
    

提交回复
热议问题