转载请注明出处: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. POJ - 1845 G - Sumdiv (唯一分解定理)

    Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...

  2. webpack对多个模块依赖进行打包

    [ webpack3.0.0刚刚出来  所以文章是跟着低版本 教程 操作熟悉  结果好多对不上喔] 七:webpack对多个模块依赖进行打包 通过一刚开始我们了解到 webpack支持commonJS ...

  3. ubuntu 安装 mysql 的正确姿势

    1.下载官方提供的mysql-apt-config.deb包进行APT源设置,下载地址:https://dev.mysql.com/downloads/repo/apt/ 2. // 将 mysql- ...

  4. HDU 6077 17多校4 Time To Get Up 水题

    Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading th ...

  5. Notepad++同一窗口显示左右显示两份文档

    Notepad++同一窗口显示左右显示两份文档   直接打开一份文档,test1.txt 再打开第二份文档,test2.txt,会显示成这样: 两份文档是以标签的形式展示的,有时需要同时查看两份文档, ...

  6. ejs-模板

    我今天第一次使用,使用的时候,遇到一些问题,还好有朋友帮我一起解决; 我先说说我使用过程中遇到的问题; 在express框架中引用 app.set('views',__dirname + '/view ...

  7. MySQL中的视图详解

    一.什么是视图? 简单来说,视图就是从一张表中导出的虚拟表.视图拥有表的结构,但是在数据库中只有视图的定义,但是没有视图中的数据. 视图是由查询语句从一张表中导出来的数据,不是一张实际的表. 二.视图 ...

  8. Shell #*/ 和 %/*

    #!/bin/bash i="this/is/a/path.config" name=${i#*/} path=${i%/*} echo $name echo $path is/a ...

  9. templates的语法

    1.变量 1.作用:将后端的数据传递到模板进行显示 2.允许作为变量的数据类型 字符串,整数,列表,元组,字典,函数,对象 3.变量的语法 变量传递给前端必须要封装到字典中才能传递给模板 1.使用 l ...

  10. [LeetCode&Python] Problem 453. Minimum Moves to Equal Array Elements

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...