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 ...
随机推荐
- 关于TP3.2微信开发那点事(基础篇)
许久没有为博客更新内容,今天我将过去一周做的微信服务号的相关心得体会在此分享,具体如何申请成为服务号的相关流程文档都有,可根据要求完成: 开发第一步:开发前配置: AppID-->微信号的&qu ...
- MMORPG大型游戏设计与开发(客户端架构 part16 of vegine)
由于近来比较忙碌和有些困倦的原因,所以关于这部分的文章没有及时更新,一句话:让朋友们久等了!今天所讲的是客户端vengine(微引擎)中最后一个部分,就像上节所说,这一部分的内容比较多.可能有些朋友看 ...
- Navicat for MySQL连接MYSQL出错,错误代码1045的解决方法
Navicat for MySQL连接MYSQL
- u3d_Shader_effects笔记4 BRDF
1.英文意思 bidirectional reflectance distribution function bidirectional :双向的 reflectance :反射 distributi ...
- luogu[1140]相似基因
题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...
- poj2580 Super Memmo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- Js实现简单的省市级联的效果
需要注意的是当需要动态添加项的时候一定要先var newoption=new Option("项","值");然后再 select.options.add(ne ...
- python日期格式化与绘图
画一个量随着时间变化的曲线是经常会遇到的需求,比如画软件用户数的变化曲线.画随时间变化的曲线主要用到的函数是matplotlib.pyplot.plot_date(date,num).由于其第一个变量 ...
- lua upvalue
转自http://blog.chinaunix.net/uid-52437-id-2108789.html Lua 中的函数是一阶类型值(first-class value),定义函数就象创建普通类型 ...
- EntityFramework Core 封装
public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IEntity ...