Modeless WinForm displays MessageBox, App hangs on exit

J

Jacob Bondski

Hello WinForm Developers,

Context: VS2003, .NETv1.1, Windows Forms, Windows 2000

After spending some time on the Internet I couldn't find the answer to
the following problem:


THE PROBLEM

Only relevant code sections shown:

public class FormOne : System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
Application.Run(new FormOne());
}

private void button_FormTwoShow_Click(object sender,
System.EventArgs e)
{
new FormTwo().Show();
}
}

public class FormTwo : System.Windows.Forms.Form
{
private void button_MessageBoxShow_Click(object sender,
System.EventArgs e)
{
MessageBox.Show("Hello World!");
}

private void button_OpenFileDialogShow_Click(object sender,
System.EventArgs e)
{
new OpenFileDialog.ShowDialog();
}
}

Consider the following scenario:

- Application displays FormOne
- User clicks button to display FormTwo
- User clicks button to display MessageBox
- User selects FormOne
- User closes FormOne

FormTwo and MessageBox remain displayed.

- User closes MessageBox
- User closes FormTwo

All UI forms are now closed, but the application hangs,
it appears that Application.Run() never returns.

(The same UI behaviour is observed when displaying the
OpenFileDialog.)

Question: how can I prevent the application from hanging?


SOLUTION ATTEMPT

The following code modification prevents FormOne from being closed
while the MessageBox is displayed, and therefore avoids the problem
described above, but introduces new problems.

Only relevant code sections shown:

public class FormOne : System.Windows.Forms.Form
{
static public FormOne s_instance; // NEW (hack)

public FormOne()
{
InitializeComponent();

s_instance = this; // NEW (hack)
}

[STAThread]
static void Main()
{
Application.Run(new FormOne()); // no modifications
}

private void button_FormTwoShow_Click(object sender,
System.EventArgs e)
{
new FormTwo().Show(); // no modifications
}
}

public class FormTwo : System.Windows.Forms.Form
{
private void button_MessageBoxShow_Click(object sender,
System.EventArgs e)
{
MessageBox.Show(FormOne.s_instance, "Hello World!"); //
MODIFICATION, providing IWin32Window argument
}

private void button_OpenFileDialogShow_Click(object sender,
System.EventArgs e)
{
new OpenFileDialog.ShowDialog(FormOne.s_instance); //
MODIFICATION, providing IWin32Window argument
}
}

Consider the following scenario:

- Application displays FormOne
- User maximises FormOne
- User clicks button to display FormTwo
- User clicks button to display MessageBox
- MessageBox is displayed, FormTwo gets hidden!
- User dismisses MessageBox
- FormTwo remains hidden, FormOne is selected!

(The same UI behaviour is observed when displaying the
OpenFileDialog.)

Problems:

1) Undesired UI behaviour
a) The application hides FormTwo when the MessageBox is displayed
b) On dismissing the MessageBox, FormTwo remains hidden
c) FormOne is selected

2) Unwanted code dependency of FormTwo on FormOne


Any help or pointers to these issues would be appreciated.

Thank you.

Jacob.
 

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