前提:

  需要引入  System.Threading;

描述:

  在很多时间我们都需要进行延迟执行,或是定时执行一些指定业务,这个时候使用 Timer 是最合适的,而且 Timer 是Cpu 级别处理对系统影响很少,就算创建上千上万个 Timer 也不会影响。

  故见意多使用 Timer 是一个很好的定时任务器。

代码:

using System;
using System.Threading; namespace MyTimer
{
class Program
{
//构建 Timer
static Timer timer = new Timer(TimerCallBack, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
static void Main(string[] args)
{
//立即执行一次
timer.Change(TimeSpan.Zero, Timeout.InfiniteTimeSpan);
Console.ReadKey();
} static void TimerCallBack(object state)
{
var nextTime = DateTime.Now.AddSeconds(10);
Console.WriteLine("{0} 执行一次,下次执行时间 {1}", DateTime.Now, nextTime);
//执行完后,重新设置定时器下次执行时间.
timer.Change(nextTime.Subtract(DateTime.Now), Timeout.InfiniteTimeSpan);
}
}
}

  

  

C# System.Threading.Timer 定时器的更多相关文章

  1. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

  2. System.Threading.Timer定时器使用注意事项

    1.定时器不要直接在方法里面定义和赋值,因为方法执行完,方法体内的变量会被GC回收. 有时候我们将timer定义在了方法里面,然后看到timer被执行了几次之后才失效,原因就是GC不一定会立即回收. ...

  3. 定时器:Timer:System.Threading.Timer类(转)

    最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...

  4. System.Threading.Timer的使用技巧

    转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...

  5. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  6. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

    System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...

  7. C# System.Threading.Timer 使用方法

    public class TimerHelper { System.Threading.Timer timer; public TaskSendMMS tasksendmms { get; set; ...

  8. System.Threading.Timer使用心得

    System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...

  9. System.Threading.Timer 使用

    //定义计时器执行完成后的回调函数 TimerCallback timecallback = new TimerCallback(WriteMsg); //定义计时器 System.Threading ...

随机推荐

  1. python3+ros api

    官方文档:https://wiki.mikrotik.com/wiki/Manual:API_Python3 # !/usr/bin/env python# -*- coding:utf-8 -*-# ...

  2. PHP - Swoole websocket理解

    php swoole实现websocket功能 1.确保安装了swoole扩展. 2.撰写服务程序 <?php //创建websocket服务器对象,监听0.0.0.0:9502端口 $ws = ...

  3. 转:Oracle里几组重要的视图--v$sysstat,v$system_event,v$parameter v$system_parameter

    按组分的几组重要的性能视图 1.System 的 over view v$sysstat , v$system_event  , v$parameter,V$instance得到oracle_sid ...

  4. python开发IO模型:阻塞&非阻塞&异步IO&多路复用&selectors

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  5. python开发模块基础:collections模块&paramiko模块

    一,collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdic ...

  6. Docker - 避免启动container后运行shell脚本执行完成后docker退出container

    问题 最近在使用 Dockerfile 启动容器,发现使用Dockerfile调用容器里面的shell,当shell执行完成以后,docker会退出容器. 分析 Docker 在执行shell的时候, ...

  7. Java 查询数据后进行递归操作

    java的递归方法记录: private List<Map<String, Object>> generateOrgMapToTree(List<Map<Strin ...

  8. 安装glibc-2.14

    下载glibc-2.14.tar.gz 解压: [root@jrgc130 software]# tar xf glibc-2.14.tar.gz [root@jrgc130 software]# c ...

  9. 关于Pycharm中如何注释

    主要有三种方法: ①在程序行前面加“#” ②将需要注释的代码用'''----''' 包起来,此方法类似C的注释方法 ③将需要注释的代码选住然后利用“Ctrl+/”进行注释 # test=input(' ...

  10. requirejs——define——普通模块

    一.普通模块可能包含的内容: 一个模块对应着一个js文件,由于模块可能包含着以下三种内容:模块名.依赖模块.返回给其他模块使用的参数:因此js文件根据包含内容的不同而写法不同. 一.传统的js脚本文件 ...