结论 *1.窗体timer和线程timer、计时器timer不同,因为后两者dispose之后,GC可以收集,而前者无法收集 *2.如果一个对象的成员函数正在被执行,那么这个对象肯定不会被收集 *3.要想对无引用的对象进行收集,就必须要终止这个对象的一切timer成员,否则无法收集该对象(因为timer正在使用该对象)。如果不停止timer就直接更改指针(这个对象彻底无法访问了),这就是新时代的内存泄露,程序早晚要内存溢出 *4.timer.stop 和timer.dispose 都会使线程终止,从而可以回收垃圾 */

 using System;
 class TimersTimer
 {
     System.Timers.Timer t;
     public TimersTimer()
     {
         Console.WriteLine("Timers.Timer is created");
         t = new System.Timers.Timer();
         t.Interval = ;
         t.Elapsed += tick;
         t.Start();
     }
     void tick(object o,EventArgs e )
     {
         t.Stop();
         Console.WriteLine("tick is called");
     }
     ~TimersTimer()
     {
         Console.WriteLine("Timers.Timer is died");
     }
 }
 class FormTimer
 {
     System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
     public FormTimer()
     {
         Console.WriteLine("Form.Timer is called");
         t.Interval = ;
         t.Tick += tick;
         t.Start();
     }
     void tick(object o, EventArgs e)
     {
         Console.WriteLine("tick is called");
         t.Dispose();
     }
     ~FormTimer()
     {
         Console.WriteLine("Form.Timer is deleted");
     }
 }
 class ThreadTimer
 {
     System.Threading.Timer t;
     public ThreadTimer()
     {
         Console.WriteLine("thread.timer is called");
         t = , );
     }
     void tick(object o)
     {
         Console.WriteLine("tick is called");
         t.Dispose();
     }
     ~ThreadTimer()
     {
         Console.WriteLine("thread.timer is died");
     }
 }
 class haha
 {
     static void Main()
     {
         new TimersTimer();
         System.Threading.Thread.Sleep();
         GC.Collect();
         System.Windows.Forms.Application.Run();
     }
 }
 /*
  * 结论
  *1.窗体timer和线程timer、计时器timer不同,因为后两者dispose之后,GC可以收集,而前者无法收集
  *2.如果一个对象的成员函数正在被执行,那么这个对象肯定不会被收集
  *3.要想对无引用的对象进行收集,就必须要终止这个对象的一切timer成员,否则无法收集该对象(因为timer正在使用该对象)。如果不停止timer就直接更改指针(这个对象彻底无法访问了),这就是新时代的内存泄露,程序早晚要内存溢出
  *4.timer.stop 和timer.dispose 都会使线程终止,从而可以回收垃圾
 */

C# has three timers的更多相关文章

  1. C# 定时器 Timers.Timer Forms.Timer

    1.定义在System.Windows.Forms里 Windows.Forms里面的定时器比较简单,只要把工具箱中的Timer控件拖到窗体上,然后设置一下事件和间隔时间等属性就可以了 //启动定时器 ...

  2. C# System.Timers.Timer的一些小问题?

    比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...

  3. [C#]System.Timers.Timer

    摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...

  4. High Precision Timers in iOS / OS X

    High Precision Timers in iOS / OS X The note will cover the do's and dont's of using high precision ...

  5. 問題排查:沒有任何多載符合 System.Timers.ElapsedEventHandler 委派

    這是在實作當前專案最後一個關鍵功能:提醒通知 所遇到的奇怪狀況 目前的設想,是以 Windows Form 結合 Timer,當作發送通知的載體 大家都知道在 C# 的環境裡,有三種內建的 Timer ...

  6. C# --System.Timers.Timer 定时方法

    注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...

  7. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  8. Qt之Timers

    简述 QObject是所有Qt objects的基类,在Qt中提供了基础定时器的支持.使用QObject::startTimer(),你可以传递一个毫秒数间隔作为参数启动一个定时器.该函数返回一个唯一 ...

  9. 使用System.Timers.Timer类实现程序定时执行

    使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...

随机推荐

  1. android MotionEvent 获取长按压时间长

    思路: 1.记录ACTION_DOWN的aX, aY坐标: 2.在ACTION_MOVE判断是否移动,移动则取消记录时间,没移动就记录: 3.记录时间,按下坐标,移动坐标分别显示在TextView a ...

  2. Python字符串的编码与解码(encode与decode)

    首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unico ...

  3. 利用div实现遮罩层效果

    利用div实现遮罩层效果就是利用一个全屏.半透明的div遮住页面上其它元素,典型的例子就是百度的登录界面.下面贴出示例代码: <!DOCTYPE html> <html> &l ...

  4. 天朝使用GAE入门指南

    0. 引言 Across the Great Wall, we can reach every corner in the world. 洒家最近玩了几下 Google App Engine.由于众所 ...

  5. STL vector

    STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...

  6. 转载 Appstore 上传被拒原因及解释

    原 apps被拒绝的各种理由以及翻译 1. Terms and conditions(法律与条款) 2. Functionality(功能) 3. Metadata (name, descriptio ...

  7. zlog学习笔记(zc_hashtable)

    zc_hashtable.h /** * hashtable */ #ifndef __zc_hashtable_h #define __zc_hashtable_h typedef struct z ...

  8. 一个screen的简单配置。。

    # Start message startup_message off defencoding utf- encoding utf- utf- shell bash hardstatus always ...

  9. QQ浏览器X5内核问题汇总

    原文:http://itindex.net/detail/53391-qq-浏览器-x5 常常被人问及微信中使用的X5内核的问题,其实我也不是很清楚,只知道它是基于android 4.2的webkit ...

  10. HTML <map> 设置图热点

    需要在一张图片中,设置一个区域为热点就用到了<map> 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. <img src="planet ...