devnull69
devnull69

Reputation: 16574

C# ShowDialog() doesn't block main form when triggered from thread

I have a Windows Forms application with a main form and several background threads. In order to show "nice" messages I implemented a form to be shown as a modal dialog using ShowDialog().

All of the calls to ShowDialog() are implemented on form level of the main form (and not on thread level), but sometimes I trigger an event from a thread which will be handled on form level of the main form. In those cases, the dialog is no longer modal ... the main form is not blocked. When the call to ShowDialog() is not triggered from a thread, it is modal as required.

How can I make sure that every call to ShowDialog() will block the main form?

Upvotes: 1

Views: 2198

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

How can I make sure that every call to ShowDialog() will block the main form?

Execute the calls to ShowDialog() on the main thread. If you need to invoke them from a different thread, use Invoke() to run them on the main thread.

Upvotes: 6

Related Questions