等待事件发生 [英] Waiting for a event to occur

查看:80
本文介绍了等待事件发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请查看下面的代码.我在屏幕上显示一个表单,然后我想等待表单关闭,然后再继续其余的代码.请注意,我无法在表单上执行ShowDialog(这是一项要求).所以我在表单上调用Show方法.

hi all,

Please look at the code below. I''m showing a form on the screen and then I would like to wait for the form to close before I continue with the rest of the code. Note that I cannot do ShowDialog (this a requirement) on the form. So I call Show method on the form.

private void button1_Click( object sender, EventArgs e )
{
  Form f = new Form( );
  f.FormClosed += new FormClosedEventHandler(f_FormClosed);
  f.Show( );
  // Here I want to wait for this form
  // to close before continuing with the rest of the code

  // Once closed, I would like to go on with the rest of code here.
  //For example:
  Console.WriteLine("My waiting is done.");

}

void f_FormClosed( object sender, FormClosedEventArgs e )
{

}



我该如何实现呢?

提前非常感谢.
/Mizan



How do I go about achiving this?

Many thanks in advance.
/Mizan

推荐答案

使用onCLos,因为这是在Close事件发生之前触发的,您可以将代码放在其中吗?
Use onCLosing as this is triggered before the Close event is, you could put your code in there?


private void button1_Click( object sender, EventArgs e )
   {
     Form f = new Form( );
     f.FormClosed += new FormClosedEventHandler(f_FormClosed);
    f.FormClosing += new FormClosingEventHandler(f_FormClosesing);
    f.Show( );


  

  }

 f_FormClosesing(object sender, FormClosingEventArgs e e)
{
 //  Write Here code which you want to run before form close;
}

  void f_FormClosed( object sender, FormClosedEventArgs e )
  {
  // your code when form closed
    Console.WriteLine("My waiting is done.");
  }


如果它是模式对话框,请在该窗体上调用ShowDialog(例如,请参见 http://msdn.microsoft. com/en-us/library/ms173179(v = vs.80).aspx [
If it is a modal dialog, call ShowDialog on that Form (e.g. see http://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.80).aspx[^]).

If you insist on working with events, see http://msdn.microsoft.com/en-us/library/ms173179(v=vs.80).aspx[^].

Cheers
Andi

PS: *Why* is it a requirement to not call ShowDialog()? You might need to elaborate on that on what issue requires you to not use that very convenient and obvious solution. Is the one requiring you to not use ShowDialog() willing to pay the cost (re-inventing the wheel, development time, testing cost, etc.)?


这篇关于等待事件发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆