开发者

Why doesn't Close() closes the form in the load event?

In my load event on a form, I call some method in a try catch block. When an Exception occurs, I show the user a message and after that I want to close the form. It looks like this (code 开发者_JAVA技巧in Load event):

try
{
   Metehod();
}
catch(DatabaseException ex)
{
  MessageBox.show("db error! " + ex.Message);
  this.Close();
}
catch(Exception ex)
{
  MessageBox.Show("Unknown error!" + ex.Message);
  this.Close();
}

But, when this.CLose() is called, it doesn't close the form, no, the code keeps running till the end of the load event!

Why is this? Is this logical behaviour?


Yes, it is logical behaviour. Calling Close() does not return control to the caller. The method will continue execution unless you tell it otherwise. Putting a return after each this.Close() does the trick.


It's by design because Load occurs before a form is displayed for the first time. So Close() has no effect since three events later in the Form life cycle, the form is shown.

To stop the cycle, use devnull solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜