[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 ...
随机推荐
- linux 下删除重复行-- uniq 与 awk
$ cat file liw liw liw hdsui mdksjd liw $ cat file | uniq -u # 只删除相邻的,不保留重复行 hdsui mdksjd liw $ cat ...
- viewport ---移动端详解
转自---http://www.cnblogs.com/2050/p/3877280.html 移动前端开发之viewport的深入理解 在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上 ...
- 修改ftp端口为50021
1. 配置 2. 增加防火墙规则 netsh firewall set portopening tcp 50021 ftp-50021 enable 3. 重启服务 net stop FileZill ...
- Linux-小命令技巧
在bash中检查远程端口是否打开:echo >/dev/tcp/8.8.8.8./53 && echo "open"将进程挂起ctrl+z,不是万能的,有程序 ...
- .Net和C#的理解
.Net是一个微软出品的开发平台,不仅仅是适用于Windows操作系统,还适用于mono,Linux等等, .Net库包括两部分:部分库定义了一些基本类型.CTS(common Type System ...
- 使用PreApplicationStartMethodAttribute
第一次见到这个东西是在公司的框架里,刚开始还挺郁闷怎么框架的Application_Start里没东西,初始化的那些代码都哪去了,后来通过一些线索找到了PreApplicationStartMetho ...
- wireshark过滤语法总结-重点偏移过滤
http://chenjiji.com/post/3371.html 作者: CHAN | 发布: 2013 年 10 月 24 日 做应用识别这一块经常要对应用产生的数据流量进行分析. 抓包采用wi ...
- 初始化Git的配置
如何使用Git上传项目代码到github http://blog.csdn.net/llf369477769/article/details/51917557
- 机器学习笔记----- ID3算法的python实战
本文申明:本文原创,如有转载请申明.数据代码来自实验数据都是来自[美]Peter Harrington 写的<Machine Learning in Action>这本书,侵删. Hell ...
- CSS3的transition动画功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...