结论 *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. Idea15/16 配置Tomcat

    1:我这里使用的是Eclipse创建的项目,导出后导入到Idea中使用 2:Idea导入Eclipse项目 3:Idea项目配置(默认快捷键CTRL+SHIT+ALT+S) 3.1 Project 项 ...

  2. jsp html5 video实现在线视频播放源码,支持IE6,7,8,10,11,谷歌,火狐等浏览器

    jsp源码: <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...

  3. 九、Android学习第八天——广播机制与WIFI网络操作(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 九.Android学习第八天——广播机制与WIFI网络操作 今天熟悉了An ...

  4. Hadoop Kernel tunning

    /etc/security/limits.conf @ochadoop soft nofile 102642 @ochadoop hard nofile 102642 @ochadoop soft n ...

  5. python paramiko

    paramiko 遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接,可以实现远程文件的上传,下载或通过ssh远程执行命令. 项目地址:https://github.com/paramik ...

  6. Sql动态添加字段的正确姿势

    如何给指定表动态添加字段? 一.创建一张表[Tbl_AutoFileds] (tableName表名,fieldName字段名,dataType数据类型,length长度  isnull 是否允许为n ...

  7. Zookeeper C API 指南四(C API 概览)(转)

    上一节<Zookeeper C API 指南三(回调函数)>重点讲了 Zookeeper C API 中各种回调函数的原型,本节将切入正题,正式讲解 Zookeeper C API.相信大 ...

  8. POJ2743Mobile Computing[DFS 状态压缩]

    Mobile Computing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 666   Accepted: 224   ...

  9. 什么是UV?

    转自http://www.cnblogs.com/jenry/p/4083415.html 1.什么是UV?   对于三维模型,有两个最重要的坐标系统,一是顶点的位置(X,Y,Z)坐标,另一个就是UV ...

  10. ORCHARD 是什么?

    官网 http://orchard.codeplex.com 教程 http://www.cnblogs.com/sunjunlin/p/3876693.html [翻译]从头开始编写一个Orchar ...