J
Joe White
Okay, here's an odd one. If I do something like this:
// First, call Application.Run()...
Application.Run(new MyMainForm());
// then show a dialog afterward
(new MyDialog()).ShowDialog();
then the call to ShowDialog() doesn't work properly: the dialog box flashes
briefly onscreen, then immediately closes. How's that for peculiar? It's
like there's some "terminated" flag that never gets reset... except that
calling Application.Run() multiple times in succession works just fine, so
clearly the terminated flag *does* get reset (at least when you're calling
Run).
I've looked briefly at the System.Windows.Forms code using Reflector, but
it's a jungle in there -- undocumented classes galore, thread contexts and
local modal message loops, exit notifications going through two or three
levels of events firing other events -- and I just don't grok it all. Does
anyone know the details well enough to have some insight into why the
ShowDialog isn't working after Run?
I know I can work around this any number of ways (I'll probably either do an
ApplicationContext descendant, or call Run() on my dialog instead of
ShowDialog()). I'm just really puzzled -- I can't see any reason why this
shouldn't work as-is.
Here's some code that does a (very) slightly more detailed test. Paste it
into your Main() method and run. (I'm running VS.NET 2003.)
// This shows up just fine...
Form dialog1 = new Form();
dialog1.Text = "Dialog (before Run)";
dialog1.ShowDialog();
// Now run the application...
Form form = new Form();
form.Text = "Form (Run)";
Application.Run(form);
// Try to show another dialog. It flashes onscreen
// and then closes immediately. Why?
Form dialog2 = new Form();
dialog2.Text = "Dialog (after Run)";
dialog2.ShowDialog();
// First, call Application.Run()...
Application.Run(new MyMainForm());
// then show a dialog afterward
(new MyDialog()).ShowDialog();
then the call to ShowDialog() doesn't work properly: the dialog box flashes
briefly onscreen, then immediately closes. How's that for peculiar? It's
like there's some "terminated" flag that never gets reset... except that
calling Application.Run() multiple times in succession works just fine, so
clearly the terminated flag *does* get reset (at least when you're calling
Run).
I've looked briefly at the System.Windows.Forms code using Reflector, but
it's a jungle in there -- undocumented classes galore, thread contexts and
local modal message loops, exit notifications going through two or three
levels of events firing other events -- and I just don't grok it all. Does
anyone know the details well enough to have some insight into why the
ShowDialog isn't working after Run?
I know I can work around this any number of ways (I'll probably either do an
ApplicationContext descendant, or call Run() on my dialog instead of
ShowDialog()). I'm just really puzzled -- I can't see any reason why this
shouldn't work as-is.
Here's some code that does a (very) slightly more detailed test. Paste it
into your Main() method and run. (I'm running VS.NET 2003.)
// This shows up just fine...
Form dialog1 = new Form();
dialog1.Text = "Dialog (before Run)";
dialog1.ShowDialog();
// Now run the application...
Form form = new Form();
form.Text = "Form (Run)";
Application.Run(form);
// Try to show another dialog. It flashes onscreen
// and then closes immediately. Why?
Form dialog2 = new Form();
dialog2.Text = "Dialog (after Run)";
dialog2.ShowDialog();