[C#]System.Timers.Timer
摘要
在.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的更多相关文章
- C# System.Timers.Timer的一些小问题?
比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...
- C# --System.Timers.Timer 定时方法
注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...
- System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
- 使用System.Timers.Timer类实现程序定时执行
使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...
- .NET System.Timers.Timer的原理和使用(开发定时执行程序)
概述(来自MSDN) Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed 事件的周期性间隔.然后可以操控此事件以提供定期处理.例如,假设您有一台关键性服务器,必须每周7 ...
- C# 定时器-System.Timers.Timer
using Newtonsoft.Json; using Rafy; using Rafy.Domain; using System; using System.Collections.Generic ...
- .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用
Windows Service(服务) 是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- C# 定时执行方法: System.Timers.Timer用法示例
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(ob ...
随机推荐
- 基础R绘图
前言: 在前面介绍了R的基础入门语法之后,现也将最近整理好的一些R的基础绘图实例提供给需要的朋友参考.(温馨提示:代码慎用!按照本博文实例进行练习的话最好能做到举一反三.代码多敲方为上策,切不可隔岸观 ...
- Linux基础3(文件权限)
文件权限 1.普通权限 (登陆用户对文件或目录的读写执行的权限) 普通权限对管理员用户无效 文件和目录 都有4中类型的用户u 所有者 : 文件.目录的创建者g 所属组 : 文件.目录属于的用户组o 其 ...
- python 学习笔记11(objgraph)
33. objgraph objgraph是Python的一个第三方包.安装之前需要安装xdot. 用途 安装 例子
- matlab实现hog特征
%%matlab实现hog特征 %修改自http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html %input: img %outp ...
- java时间和日期类型
在java中,代表时间和日期的类型包括:java.util.Date和java.util.Calendar,此外,在JDBC API中还提供了3个扩展类,java.UtilDate类的子类:java. ...
- BZOJ2005 莫比乌斯反演
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 实际上把这些被挡住的点的坐标和能量值列举出来可以发现有个公式: “对于坐标系第一象限任意 ...
- checking for tgetent()... configure: error: NOT FOUND!
今天centos出现了下面的异常: checking for tgetent()... configure: error: NOT FOUND! You need to install a termi ...
- AngularJs angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- 无法启动此程序,因为计算机中丢失MSVCP110.dll
安装Visual C++ Redistributable for Visual Studio 2012 有arm.x86.x64有三个版本. 如果应用程序为debug版本而不是release版本,可能 ...
- 掘金chrome插件
掘金chrome插件 点击下载 掘金是一个高质量的互联网技术社区,而其提供的一个chrome插件个人觉得非常不错.最终效果如下所示: 每天都会有优秀的内容更新.