本文介绍了等待事件发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好, 请查看下面的代码.我在屏幕上显示一个表单,然后我想等待表单关闭,然后再继续其余的代码.请注意,我无法在表单上执行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(例如,请参见 msdn.microsoft/zh-CN/library/c7ykbedk(v = vs.80).aspx [ ^ ]). 如果您坚持使用事件,请参见 msdn.microsoft. com/en-us/library/ms173179(v = vs.80).aspx [ ^ ]. 干杯 安迪 PS:*为什么*要求不调用ShowDialog()?您可能需要详细说明哪些问题需要您不使用那种非常方便和明显的解决方案.是不是要求您不愿意使用ShowDialog()来支付费用(重新发明轮子,开发时间,测试成本等)的人? If it is a modal dialog, call ShowDialog on that Form (e.g. see msdn.microsoft/en-us/library/c7ykbedk(v=vs.80).aspx[^]). If you insist on working with events, see msdn.microsoft/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.)?
更多推荐
等待事件发生
发布评论