System.Timers.Timer,System.Timers.Timer在使用的过程中需要:

1、构造函数不同,构造函数可以什么事情也不做,也可以传入响应间隔时间:System.Timers.Timer timer = new System.Timers.Timer(10);

2、设置响应事件的响应函数:timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

3、调用 timer.Start()或者timer.Enabled = true来启动它, timer.Start()的内部原理还是设置timer.Enabled = true

4、调用 timer.Stop()或者timer.Enabled = false来停止引发Elapsed事件, timer.Stop()的内部原理还是设置timer.Enabled = false,最重要的是timer.Enabled = false后会取消线程池中当前等待队列中剩余任务的执行。

看起来还有点麻烦,那我们就尝试封装一个自己的Timer类,在内部实现Timer。代码如下:

public class MyTimer
{
private System.Timers.Timer timeOut = new System.Timers.Timer();
public delegate void Delegate_TimeOut(object tag);
public event Delegate_TimeOut delegate_timeOut;
private object tag; public MyTimer(double duration)
{
timeOut.Interval = duration;
timeOut.Elapsed += new System.Timers.ElapsedEventHandler(timeOut_Elapsed);
} public void Start(object o)
{
this.tag = o;
timeOut.Enabled = true;
timeOut.Start();
} public void Stop()
{
timeOut.Enabled = false;
timeOut.Stop();
} ~MyTimer()
{
timeOut.Elapsed -= new System.Timers.ElapsedEventHandler(timeOut_Elapsed);
timeOut.Close();
} private void timeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//do what you want to do
delegate_timeOut(tag);
}
}

使用:

MyTimer myTimer = new MyTimer();
myTimer.delegate_timeOut += new MyTimer.Delegate_TimeOut(overTime);//overTime 为具体实现 myTimer.Start(null);
 

封装Timer的更多相关文章

  1. java Timer定时器管理类

    1.java timer类,定时器类.启动执行定时任务方法是timer.schedule(new RemindTask(), seconds*1000);俩参数分别是TimerTask子类,具体执行定 ...

  2. javascript编写的一个完整全方位轮播图效果

    1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...

  3. react native中的聊天气泡以及timer封装成的发送验证码倒计时

    今天看来情书写的文章,研究了一下大佬写的文章,自己做一点总结. 其实,今天我想把我近期遇到的坑都总结一下:1.goBack的跨页面跳转,又两种方法,一可以像兔哥那样修改navigation源码,二可以 ...

  4. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  5. 原生JS封装Ajax插件(同域&&jsonp跨域)

    抛出一个问题,其实所谓的熟悉原生JS,怎样的程度才是熟悉呢? 最近都在做原生JS熟悉的练习... 用原生Js封装了一个Ajax插件,引入一般的项目,传传数据,感觉还是可行的...简单说说思路,如有不正 ...

  6. 关于Quartz.NET作业调度框架的一点小小的封装,实现伪AOP写LOG功能

    Quartz.NET是一个非常强大的作业调度框架,适用于各种定时执行的业务处理等,类似于WINDOWS自带的任务计划程序,其中运用Cron表达式来实现各种定时触发条件是我认为最为惊喜的地方. Quar ...

  7. 各种Js封装

    获取ClassName元素 function getClass(classname,id){ if(document.getElementsByClassName){ if(id){ return $ ...

  8. GLUT的简洁OO封装

    毕业设计用到了OpenGL,由于不会用MFC和Win32API做窗口程序:自然选用了GLUT.GLUT很好用,就是每次写一堆Init,注册callback,觉得有点恶心,于是对他做了简单的OO封装.记 ...

  9. 模拟CSS3 多组位移运动属性的框架封装

    obj是将要运动的对象,json是运动完成时的位移结果. 封装要点: 1.定时器开关flag的定义要放在for in结构的外面,否则,每遍历一次,都会定义一个 新的flag 2.if(current ...

随机推荐

  1. js 的 提交

    <script type="text/javascript"> function sub(){ if(document.form1.xingming.value==&q ...

  2. HDOJ1002题A + B Problem II,2个大数相加

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  3. GPG error: the public key is not available

    GPG error: The following signatures couldn't be verified because the public key is not available I h ...

  4. HDU 3078 Network LCA

    题意:n个点 m个询问,下面一行是n 个点的权值 再下面n-1行是双向的边 然后m个询问:k u v 若k==0,则把u点的权值改为v,否则回答u->v之间最短路经过点的权值中  第k大的值是多 ...

  5. 一些技术blog和安全blog

    1.安全blog: http://zenxds.com/blog/ http://navisec.it/ http://huaidan.org/ http://leapar.lofter.com/ h ...

  6. web项目学习之sitemesh

    sitemesh主要有三个主要文件:sitemesh.xml,decorators.xml和做布局用的jsp页面. 一.sitemesh.xml 对于sitemesh.xml这个文件,官方文档上说它不 ...

  7. Android color(颜色) 在XML文件和java代码中

    Android color(颜色) 在XML文件和java代码中,有需要的朋友可以参考下. 1.使用Color类的常量,如: int color = Color.BLUE;//创建一个蓝色 是使用An ...

  8. 算法----希尔排序(shell sort)

    在分析插入排序(插入排序算法实现)的算法性能的过程时知道.当数组规模较小或者存在较多的有序子序列时.插入排序将会在非常短的时间内完毕数组的排序,为此能够设计一个单调序列h[n],将数组分为多个小的序列 ...

  9. 获取文件CRC和MD5

    unit untCRCMD5; interface { 获取文件CRC校验码 } function GetFileCRC(const iFileName: string): String; { 获取字 ...

  10. 重复记录(duplicate records)相关运营数据

    MySQL 中查找反复数据,删除反复数据 创建表和測试数据 /* 表结构 */ DROPTABLEIFEXISTS `t1`; CREATETABLEIFNOTEXISTS `t1`( `id` IN ...