日期:2014-05-17  浏览次数:21253 次

怎么彻底关闭以ShowDialog()显示的窗体
按下MainForm里的一个按钮后,以ShowDialog()的方式显示SetCodeForm,我遇到的问题是,即使用Dispose的方法关闭了SetCodeForm,其内部的代码也没有结束,SetCodeForm内的timer控件还是会触发事件,代码如下:

//在MainForm里有如下代码
SetCodeForm fmSetCode;//申明为类成员
private void lbSetCode_Click(object sender, EventArgs e)
{
            fmSetCode = new SetCodeForm();
            if (fmSetCode.ShowDialog() == DialogResult.Cancel)
            {
                fmSetCode.Dispose();
            }
}


//在SetCodeForm里有如下代码
 private void buttonClose_Click(object sender, EventArgs e)
{
       this.Dispose();//关闭窗体
}
 private void timerFail_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {//触发时间设置的5秒
            timerFail.Stop();
            MessageBox.Show("修改失败!", "错误提示");
            btnOK.Enabled = true;
 }


我的问题是,关闭了SetCodeForm后,还是会执行timerFail_Elapesed事件,求问怎么彻底关闭SetCodeForm,关闭后,其内部的代码不会再执行。
ShowDialog() Dispose()

------解决方案--------------------
用Forms下的那个Timer窗体关闭就肯定不工作了。
------解决方案--------------------

我只是菜鸟····
------解决方案--------------------
因为你用自己的代码把System.Timer加到form中后,form dispose时,估计不知道该去调用timer 的dispose。timer一般用在后台没界面的时候多。
换成timer控件应该没这个问题了。
------解决方案--------------------
引用:
额,楼上就是帮我解答的高手,我把分结给他不违规的吧。

你是用Timers下的那个Timer?要是的话,他跟线程没区别的,好比线程的前台线程与后台线程,
前台线程就算你关了窗口,他还会继续执行,后台线程随窗口关闭就停止一个道理,你改用Forms下的Timer吧,这个随窗体关闭自动停止