C# has three timers
结论 *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的更多相关文章
- C# 定时器 Timers.Timer Forms.Timer
1.定义在System.Windows.Forms里 Windows.Forms里面的定时器比较简单,只要把工具箱中的Timer控件拖到窗体上,然后设置一下事件和间隔时间等属性就可以了 //启动定时器 ...
- C# System.Timers.Timer的一些小问题?
比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...
- [C#]System.Timers.Timer
摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...
- 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 ...
- 問題排查:沒有任何多載符合 System.Timers.ElapsedEventHandler 委派
這是在實作當前專案最後一個關鍵功能:提醒通知 所遇到的奇怪狀況 目前的設想,是以 Windows Form 結合 Timer,當作發送通知的載體 大家都知道在 C# 的環境裡,有三種內建的 Timer ...
- C# --System.Timers.Timer 定时方法
注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...
- System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
- Qt之Timers
简述 QObject是所有Qt objects的基类,在Qt中提供了基础定时器的支持.使用QObject::startTimer(),你可以传递一个毫秒数间隔作为参数启动一个定时器.该函数返回一个唯一 ...
- 使用System.Timers.Timer类实现程序定时执行
使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...
随机推荐
- 内部类访问的局部变量必须加final
(1)内部类是外部类的一个成员,就像外部类的成员方法一样,所以内部类有权限访问外部类的所有成员,包括private的. (2)内部类不能访问外部类方法中的局部变量,除非变量是final的(一般发生在 ...
- Java设计模式之代理模式
代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式: 人如其名,代理相 ...
- ZooKeeper开发手册中文翻译(转)
本文Github地址:https://github.com/sundiontheway/zookeeper-guide-cn 本文假设你已经具有一定分布式计算的基础知识.你将在第一部分看到以下内容: ...
- Zookeeper C API 指南四(C API 概览)(转)
上一节<Zookeeper C API 指南三(回调函数)>重点讲了 Zookeeper C API 中各种回调函数的原型,本节将切入正题,正式讲解 Zookeeper C API.相信大 ...
- AC日记——质因数分解 1.5 43
43:质因数分解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...
- ANE接入平台心得记录(安卓)
开发环境:FlashBuilder4.7 AIR13.0 Eclipse 由于我懒得陪安卓的开发环境所以我下载了包含安卓SDK Manager的Eclipse,其实直接用FlashBuilder开发A ...
- Eclipse自动打开实现类原型的工具
http://eclipse-tools.sourceforge.net/implementors/ I always use this implementors plugin to find all ...
- MVC 表单提交【转】
[转自]:http://www.cnblogs.com/dengdl/archive/2011/07/14/2106849.html 在做Asp.Net MVC项目中,都知道View负责页面展示数据或 ...
- 查看oracle死锁进程并结束死锁
查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo ...
- PHP 图片处理PNG颜色丢失
根据需求做一个用户点击测试桃花运的小程序.在开发中需要使用PHP进行开发,原理是将用户的姓名通过php的图片处理写入图片中,此处遇到一巨坑. 就是png图片在调用 imagecolorallocate ...