views:

223

answers:

1

The problem I am having is that for some reason ShowDialog returns immediately after handle an exception. However, it works for MessageBox.Show() or at the second time I call ShowDialog. What is the best workaround for this problem?

I was only able to find this. And I cannot believe that no one else has this issue.

I am using .net compact framework 3.5. Sample code:

try
{
  using(SomeForm f = new SomeForm())
  {
    f.ShowDialog();
  }
}
catch(SomeException)
{
  using(SomeOtherForm f = new SomeOtherForm())
  {
    f.ShowDialog(); // this returns immediately
                    // if this is MessageBox.Show(), it works correctly.
    f.ShowDialog(); // then this works fine
  }
}
+1  A: 

I just answered the following in another question, which I think is related to the same problem. Although in your case 2) might not work (unless manually blocking the thread).

I came across this problem as well. This is a known issue in .NET CF (v2.0), but I also had it while using v3.5 (although the situations in which it occurs are more specific). You can find the (old and still active) bug report here.

Calling MessageBox.Show() causes it to close immediately, but in my case there were two workarounds:

1) Call the MessageBox.Show() a second time. It then does block until closed by the user. You can check the first MessageBox.Show() closed prematurely by checking the DialogResult. I don't remember which result it returned exactly when it failed, I remember it giving a non-default result.

2) Create a custom Form and call ShowDialog() on that. It worked for me, but others have reported it doesn't work. You could also call Show() and make it blocking yourself (don't forget to call Application.DoEvents() so it keeps processing events).

Martijn Stolk
Last week, I found this answer which solves the problem. http://stackoverflow.com/questions/724644/windowsmobile-application-exits-after-handling-exception-from-dialogform
leiz