Where can I find details about modal vs modeless implementations of Show() vs ShowDialog(). Up until now I always assumed that calling a modeless implementation would create a separate thread but I cannot find any source to back this up or infirm it.
-
Backgrounder answer [is here](http://stackoverflow.com/questions/5181777/use-of-application-doevents/5183623#5183623) – Hans Passant Apr 04 '13 at 13:13
-
Neither starts a new thread. Both work off the current message pump (UI thread). – Peter Ritchie Apr 04 '13 at 14:48
-
FYI if you don't call `Application.Run` manually on a different thread, you won't have two threads handling UI messages. – Peter Ritchie Apr 04 '13 at 14:49
-
What problem are you trying to solve? – Cody Gray - on strike Apr 05 '13 at 05:45
-
@Cody Gray: not trying to solve anything but only trying to clarify my understanding – BlueTrin Apr 05 '13 at 13:44
1 Answers
Show
and ShowDialog
will never create a new thread. As you are probably aware, all UI elements are hosted on one and only one thread. Normally this thread is the one that called Application.Run
. UI forms and controls require a message loop in order to operate correctly. Application.Run
is one of the mechanisms (and the most common) for creating this message loop.
What is interesting about modal dialogs is that ShowDialog
will create its own message loop for the dialog. This is why it will typically work1 even when called from a thread other than the main UI thread.
So to answer your question: neither will create a new thread, but ShowDialog
will create its own message loop.
1I use the term "work" loosely here. Yes, it will display properly, respond to most of the user actions, etc. However, it is not recommended that you do this because it can cause problems elsewhere and may even behave strangely. See above rule that all UI elements should be hosted by a single thread.

- 239,200
- 50
- 490
- 574

- 47,849
- 13
- 107
- 150
-
1The `ShowDialog` method will always run a new message loop for the modal dialog. I can't think of a single exception to this, so I went ahead and updated your otherwise excellent answer. If there's a particular reason or some kind of edge case that prompted you to hedge your bets with a "may", please feel free to roll back my edits and leave me a courtesy ping. – Cody Gray - on strike Apr 05 '13 at 05:47
-
@CodyGray: Good update. To be brutally honest I didn't even think about `ShowDialog` *always* creating a message pump. But, now that you mention it the fact should have been obvious. It just goes to show that there is always something new be learned! – Brian Gideon Apr 05 '13 at 12:49