Modal dialog after Application.Run returns

H

Harold Howe

I would like to display a modal dialog after Application.Run returns. In
this particular case, it is a dialog to inform the user that some
cleanup code suffered catastrophic failure.

I have discovered that forms cannot be shown modally once Run returns.
Is there a way to alter this behavior? What is the best way to show a
dialog when the app is in the process of shutdown? Am I doing something
wrong?

Here is a short example of the problem.

using System;
using System.Windows.Forms;

namespace Test
{
class Test
{
static void Main()
{
Form f = new Form();
f.Text = "Main form";

Application.Run(f);

f = new Form();
f.Text = "Goodbye form.";
f.ShowDialog();
}
}
}

The goodbye dialog appears briefly on the screen, and then disappears.
I don't understand why the framework doesn't allow this. It is possible
to show forms modally before Run, but not after.

The workarounds I have found are to replace f.ShowDialog with

Application.Run(f); // just call Run again

f.Show()
while(f.Visible)
Application.DoEvents();

I would prefer to not have to resort to either of these, but may not
have a choice. Any suggestion?

H^2
 
H

Herfried K. Wagner [MVP]

Harold,

Harold Howe said:
I would like to display a modal dialog after Application.Run returns. In
this particular case, it is a dialog to inform the user that some cleanup
code suffered catastrophic failure.
[...]
Application.Run(f);

f = new Form();
f.Text = "Goodbye form.";
f.ShowDialog();
}
}
}

The goodbye dialog appears briefly on the screen, and then disappears.
I don't understand why the framework doesn't allow this. It is possible to
show forms modally before Run, but not after.

The application is lacking a message pump after returning from
'Application.Run'. 'ShowDialog' doesn't make much sense here because the
form is not shown modally to another form. As you already say, you can use
'Application.Run' a 2nd time. In addition to that, you may want to use
'ApplicationContext'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top