2个窗体:A,B ; 
由A打开B,然后启动B中的timer
timer事件中将B中的(public static int) num ++
然后关闭B,但是不关闭timer
再在A中观察B.num,发现B.num还在继续自增,也就是说B中的timer还在继续,这是为什么?

解决方案 »

  1.   

    应该是在main中已经初始化了窗体A和B了,所有窗体关闭系统才会释放资源吧
      

  2.   

    B还没有及时被垃圾回收。
    在B关闭之前将timer停掉
      

  3.   

    在B窗口的关闭事件下头把  timer给stop了
      

  4.   

    C#里面资源的由。net来管理的
      

  5.   

    Close没有释放资源.需要dispose一下..
      

  6.   

    测试了一下。没发现这种情况。你贴出你测试代码吧。
    你用的哪个timer?
    System.Windows.Forms.Timer是基于消息的,当宿主控件消失了。应该就无法接受消息,不会再次触发tick事件了。
    看了你代码才知道。
      

  7.   

    Form1中:         private void button2_Click(object sender, EventArgs e)
            {
                //用来观察TestNum变化
                MessageBox.Show(FormTimer.TestNum.ToString());
            }        private void button1_Click(object sender, EventArgs e)
            {
                FormTimer tim = new FormTimer();
                tim.ShowDialog();
            }Form2中:
    time1为System.Windows.Forms.Timer        public static int TestNum = 0;        private void timer1_Tick(object sender, EventArgs e)
            {
                TestNum++;
            }        private void btn_Start_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
            }        private void btn_End_Click(object sender, EventArgs e)
            {
                timer1.Enabled = false;
            }大概就是这些,都是拖出来的控件
      

  8.   

    这个和静态属性没关系吧,我只是拿这个静态属性来观察timer是否还在继续
      

  9.   

    如果知道了C#的事件是用观察者模式设计的, 那么就容易理解多了。你在Timer上挂了事件, 并且delegate放在了B上。
    根据观察者模式, 实际上会将timer的引用, 存放到B中。
    (发出事件者会在接受事件者那里保存一份自己的引用)然后, 只要你的B没有被回收, 那么timer就永远被B抓着。
    那么, timer永远不会被回收。
    直到B被回收了, timer才彻底成了“无家可归的孩子”, 才会被回收。
    那时候timer可能才会停。
      

  10.   

    form.ShowDialog()这样打开的窗口在在点击"X"的时候,窗体是不会销毁的,只是隐藏掉了