Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c# - WindowsMobile: Application Exits after handling Exception from DialogForm

I have the following simple scenario:

A DialogForm with a Button, the Button_click throws an exception.

A MainForm with a button and a Label, in the click I show a new instance of the DialogForm inside a Catch block.

If I run this setup in regular WinForms, I can catch the Exception as expected.

If I run this in WinMobile (I've tested on WM5 and WM6 Pro) I can see with a Debugger that the Catch block is entered but the Exception keeps propagating up and the App dies.

The code in MainForm looks like this:

try
{
   using (DialogForm frm = new DialogForm())
   {
     DialogResult r = frm.ShowDialog();
     label1.Text = r.ToString();
  }
}
catch (Exception ex)
{
  label1.Text = ex.Message;
}

Edit:

I investigated a little further, with a catch {} block around this code and around Application.Run() and the app still quits.

Apparently it is not a runaway Exception, that is caught and handled just fine. But after this operation it looks like the Application performs an unwanted Exit().

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After tinkering on I found something that works:

try {
  // show Dialog that Throws
}
catch (Exception ex) {
  label1.Text = ex.Message;
  Application.DoEvents();  // this solves it
}

The bounty is still open for anyone who can tell me why the DoEvents() is necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...