转载请注明出处: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. Intellij IDEA生成foreach或者loop 快捷键

    iter Iterate (for each..in) itin Iterate (for..in) itli Iterate over a List itar Iterate elements of ...

  2. 牛客多校第四场 G Maximum Mode

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...

  3. Flask源码阅读-第一篇(flask包下的__main__.py)

    源码: # -*- coding: utf-8 -*-""" flask.__main__ ~~~~~~~~~~~~~~ Alias for flask.run for ...

  4. HDU 6045 17多校2 Is Derek lying?

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  5. 4.App测试与Web测试的不同

    注释:*蓝色为不同点,红色为测试类型* 测试工具不同 Web自动化用Selenium APP自动化用Appium 软件架构不同 App为C/S架构 Web为B/S架构 需要进行安装卸载更新测试 第一次 ...

  6. Too much thinking! Too much annoying.

    I am now in great demand for an opportunity to yearn for, the ability to express myself, in a maximu ...

  7. php 怎样将有范围的ip转化为整型范围

    php中将IP转换成整型的函数ip2long()容易出现问题,在IP比较大的情况下,会变成负数.如下: <?php $ip = "192.168.1.2"; $ip_n = ...

  8. mastering elasticsearch

    概念: Term: 它是搜索的基本单位,其表现形式为文本中的一个词. Token: 它是单个Term在所属Field中文本的呈现形式,包含了Term内容.Term类型.Term在文本中的起始及偏移位置 ...

  9. oracle死锁测试

    测试一个死锁,看看oracle会报什么错误,以及oracle如何处理 1.创建测试表,测试数据 2.第一轮update,delete 两个会话,不同的行 3.第二轮,delete,update两个会话 ...

  10. event.currentTarget和event.target的区别

    currentTarget始终是监听事件者,而target是事件的真正发出者.