转载请注明出处:http://www.cnblogs.com/KeenLeung/p/3911556.html

Timer类:设置一个定时器,定时执行用户指定的函数。

定时器启动后,系统将自动建立一个新的线程,执行用户指定的函数。

初始化一个Timer对象:

Timer timer = new Timer(timerDelegate, s,1000, 1000);

// 第一个参数:指定了TimerCallback 委托,表示要执行的方法;

// 第二个参数:一个包含回调方法要使用的信息的对象,或者为空引用;

// 第三个参数:延迟时间——计时开始的时刻距现在的时间,单位是毫秒,指定为“0”表示立即启动计时器;

// 第四个参数:定时器的时间间隔——计时开始以后,每隔这么长的一段时间,TimerCallback所代表的方法将被调用一次,单位也是毫秒。指定 Timeout.Infinite 可以禁用定期终止。

Timer.Change()方法:修改定时器的设置。(这是一个参数类型重载的方法)

使用示例: timer.Change(1000,2000);

Timer类的程序示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace ConsoleApplication1
{
    class TimerExampleState
    {
        public int counter = 0;
        public Timer tmr;
    }
    class Program
    {
        static void Main(string[] args)
        {
            TimerExampleState s = new TimerExampleState();
            //创建代理对象TimerCallback,该代理将被定时调用
            TimerCallback timerDelegate = new TimerCallback(CheckStatus);             //创建一个时间间隔为1s的定时器
            Timer timer = new Timer(timerDelegate, s, 1000, 1000);
            s.tmr = timer;             //主线程停下来等待Timer对象的终止
            while (s.tmr != null)
                Thread.Sleep(0);
            Console.WriteLine("Timer example done.");
            Console.ReadLine();
        }
        //下面是被定时调用的方法
        static void CheckStatus(Object state)
        {
            TimerExampleState s = (TimerExampleState)state;
            s.counter++;
            Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, s.counter);             if (s.counter == 5)
            {
                //使用Change方法改变了时间间隔
                (s.tmr).Change(10000, 2000);
                Console.WriteLine("changed");
            }             if (s.counter == 10)
            {
                Console.WriteLine("disposing of timer");
                s.tmr.Dispose();
                s.tmr = null;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace ConsoleApplication1
{
    class TimerExampleState
    {
        public int counter = 0;
        public Timer tmr;
    }
    class Program
    {
        static void Main(string[] args)
        {
            TimerExampleState s = new TimerExampleState();
            //创建代理对象TimerCallback,该代理将被定时调用
            TimerCallback timerDelegate = new TimerCallback(CheckStatus);             //创建一个时间间隔为1s的定时器
            Timer timer = new Timer(timerDelegate, s, 1000, 1000);
            s.tmr = timer;             //主线程停下来等待Timer对象的终止
            while (s.tmr != null)
                Thread.Sleep(0);
            Console.WriteLine("Timer example done.");
            Console.ReadLine();
        }
        //下面是被定时调用的方法
        static void CheckStatus(Object state)
        {
            TimerExampleState s = (TimerExampleState)state;
            s.counter++;
            Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, s.counter);             if (s.counter == 5)
            {
                //使用Change方法改变了时间间隔
                (s.tmr).Change(10000, 2000);
                Console.WriteLine("changed");
            }             if (s.counter == 10)
            {
                Console.WriteLine("disposing of timer");
                s.tmr.Dispose();
                s.tmr = null;
            }
        }
    }
}

程序首先创建了一个定时器,它将在创建1秒之后开始每隔1秒调用一次CheckStatus()方法,当调用5次以后,在CheckStatus()方法中修改了时间间隔为2秒,并且指定在10秒后重新开始。当计数达到10次,调用Timer.Dispose()方法删除了timer对象,主线程于是跳出循环,终止程序。

C# 线程:定时器的使用的更多相关文章

  1. Delphi线程定时器TThreadedTimer及用法--还有TThreadList用法可以locklist

    Delphi线程定时器 - -人生如歌- - 博客园http://www.cnblogs.com/zhengwei0113/p/4192010.html (* 自己编写的线程计时器,没有采用消息机制, ...

  2. python线程定时器Timer(32)

    相对前面几篇python线程内容而言,本片内容相对比较简单,定时器 – 顾名思义,必然用于定时任务. 一.线程定时器Timer原理 原理比较简单,指定时间间隔后启动线程!适用场景:完成定时任务,例如: ...

  3. C# 根据自定义线程定时器 生成随机订单

    这个源之于一个朋友问我的一个问题,他说他们的需求是在一天之内随机抽取数据生成订单,还不能让客户看出来. 随机生成的订单还分概率抽取不一定的状态值,那么根据我之前写的定时器线程执行器,我们设计需要一个定 ...

  4. (16)线程---定时器Timer

    # ### 定时器:指定时间执行任务 from threading import Timer def func(): print("目前正在执行任务") t = Timer(5,f ...

  5. 使用boost线程定时器作为后台线程来切换主循环程序状态方法2

    上一篇的方法主要使用的是:通过线程延时实 现的定时,并且只能定时一次,如果需要对此定时处理,就需要使用下面的定时器: #include "stdafx.h" #include &l ...

  6. Java——线程定时器

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  7. JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题

    JAVA基础再回首(二十五)--Lock锁的使用.死锁问题.多线程生产者和消费者.线程池.匿名内部类使用多线程.定时器.面试题 版权声明:转载必须注明本文转自程序猿杜鹏程的博客:http://blog ...

  8. iOS - OC NSTimer 定时器

    前言 @interface NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的,添加 ...

  9. iOS - Swift NSTimer 定时器

    前言 public class NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的, ...

  10. c++11实现异步定时器

    c++11提供了丰富的时间和线程操作函数,比如 std::this_thread::sleep, std::chrono::seconds等.可以利用这些来很方便的实现一个定时器.     定时器要求 ...

随机推荐

  1. linux 清空文件

    将Linux文件清空的几种方法 1.使用重定向的方法 [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > tes ...

  2. 2018ICPC青岛 E - Plants vs. Zombies (二分+模拟)

    ZOJ - 4062 题意:有n个植物排成一排,按顺序植物的编号是1-n,每个植物都有一个生长速率,有一个机器人,机器人可以走m步,每走一步,这个机器人就会浇一次水,浇一次水那个植物就会长 自身的生长 ...

  3. C++类构造函数初始化列表(转)

    构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式.例如: { public:     int a;     float b;     //构 ...

  4. python 递归进阶操作方法

    递归 在函数内部,可以调用其他函数; 如果一个函数在内部调用自身本身,这个函数就是递归函数. 例如,我们来计算阶乘: n! = 1 x 2 x 3 x ... x n, 用函数f1(n)表示,可以看出 ...

  5. Python基础4--一看就会的选择与循环

    1 选择 if elif else 注意后面均有: if age>18: print 'adult' elif age>6: print 'teenager' else: print 'k ...

  6. JAVA_模糊查询_重点是concat关键字

    SELECT * FROM user WHERE username LIKE concat('%',#{username},'%') concat : 类似+ ,拼接sql.sql语句中会将+ 重写. ...

  7. HDU 1213 How Many Tables(并查集裸题)

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  8. HDU 6095 17多校5 Rikka with Competition(思维简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  9. RIP路由协议(一)

    实验要求:使用RIPv2配置路由器,使路由器能接收到所有的路由条目 拓扑如下: 配置如下: R1enable 进入特权模式configure terminal 进入全局模式interface s0/0 ...

  10. phpcms 新建模块安装

    1.安装配置---小问题: 估计就我这么傻 T-T  ,改成自己的目录名. 2.模块的目录: 模块存放在modules文件夹里,打开这个文件夹,里面的一个文件夹代表一个模块. 3.建立模块以及其基本目 ...