摘要

在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架,用Timer完全可以满足要求。

一个例子

每一秒在控制台上打印时间。

    class Program
{
static void Main(string[] args)
{
var timer = new System.Timers.Timer();
timer.Elapsed += timer_Elapsed; timer.AutoReset = true;
timer.Enabled = true;
timer.Interval = ;
Console.Read();
} private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine(e.SignalTime.ToString());
}
}

timer.AutoReset = true;注意,AutoReset属性,如果你希望到时间了,不停的执行Elapsed事件,要将其设置为true。它的作用类似js中的setInterval方法,如果为false,类似于js中的setTimerout方法,只执行一次。

所以在使用timer的时候,你要考虑到业务需求,是执行一次,还是不停的执行。

[C#]System.Timers.Timer的更多相关文章

  1. C# System.Timers.Timer的一些小问题?

    比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...

  2. C# --System.Timers.Timer 定时方法

    注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...

  3. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  4. 使用System.Timers.Timer类实现程序定时执行

    使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...

  5. .NET System.Timers.Timer的原理和使用(开发定时执行程序)

    概述(来自MSDN) Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed 事件的周期性间隔.然后可以操控此事件以提供定期处理.例如,假设您有一台关键性服务器,必须每周7 ...

  6. C# 定时器-System.Timers.Timer

    using Newtonsoft.Json; using Rafy; using Rafy.Domain; using System; using System.Collections.Generic ...

  7. .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...

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

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

  9. C# 定时执行方法: System.Timers.Timer用法示例

    System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒        private void Form1_Load(ob ...

随机推荐

  1. 【bzoj1082】 SCOI2005—栅栏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1082 (题目链接) 题意 给出m块木柴,以及n块木板,要求将m块木柴做木板,要求将木柴切割成与木板一 ...

  2. Myeclipse的show in breadcrumb

    m如何取消eclipse的show in breadcrumb 不小心点了show in breadcrumb,在编辑器界面上面多一层路径条,多余碍事,不晓得怎么取消,搞了半天终于弄好,方法如下: 点 ...

  3. 【Alpha版本】 第六天 11.14

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成管理员的三大界面框架.完成管理主界面 完成我要招聘的招聘详情显 ...

  4. Python – locals和globals

    转载: Python两个内置函数--locals 和globals (学习笔记) Python两个内置函数locals 和globals, 这两个函数主要提供,基于字典的访问局部和全局变量的方式.在理 ...

  5. IOS基础之 (设计模式)

    一 工厂方法 工厂方法方便我们快速创建类的实例的方法.通过工厂方法,可以让调用过程更加清晰. Person.h #import <Foundation/Foundation.h> @int ...

  6. UIViewController 的 presentedViewController 和 presentingViewController

    #import "TestViewController.h" #import "OneViewController.h" 在TextViewController ...

  7. Processing Images

    https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_ ...

  8. wpf arcglobe +c# 三维缩放到图层

    /// <summary>        /// 地图缩放到图层        /// </summary>        /// <param name="s ...

  9. PHP中静态与抽象的概念

    静态//普通成员//普通成员是属于对象的 //静态成员//静态成员属于类的 类中的静态属性非常类似于函数的全局变量.类中的静态成员是不需要对象而使用类名来直接访问的. //关键字:static//se ...

  10. MongoDB的安装及CURD操作

    MongoDB的下载地址:http://www.mongodb.org/downloads MongoDB有32bit和64bit两个版本,32bit只能存放2GB数据.我们选择64bit版进行下载. ...