c# 多线程之-- System.Threading Timer的使用
作用:每隔多久去执行线程里的方法.
class ThreadTimerDemo
{
static void Main(string[] args)
{
// Create an AutoResetEvent to signal the timeout threshold in the
// timer callback has been reached.
var autoEvent = new AutoResetEvent(false); var statusChecker = new StatusChecker(); // Create a timer that invokes CheckStatus after one second,
// and every 1/4 second thereafter.
Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
DateTime.Now);
var stateTimer = new Timer(statusChecker.CheckStatus,
autoEvent, , ); // When autoEvent signals, change the period to every half second.
autoEvent.WaitOne();
stateTimer.Change(, );
Console.WriteLine("\nChanging period to .5 seconds.\n"); // When autoEvent signals the second time, dispose of the timer.
autoEvent.WaitOne();
stateTimer.Change(, );
Console.WriteLine("\nChanging period to 1 seconds.\n");
autoEvent.WaitOne();
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
Console.ReadKey();
}
class StatusChecker
{
private int invokeCount;
private int maxCount; public StatusChecker(int count)
{
invokeCount = ;
maxCount = count;
} // This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString()); if (invokeCount == maxCount)
{
// Reset the counter and signal the waiting thread.
invokeCount = ;
autoEvent.Set();
}
}
}
}
// The example displays output like the following:
// 11:59:54.202 Creating timer.
//
// 11:59:55.217 Checking status 1.
// 11:59:55.466 Checking status 2.
// 11:59:55.716 Checking status 3.
// 11:59:55.968 Checking status 4.
// 11:59:56.218 Checking status 5.
// 11:59:56.470 Checking status 6.
// 11:59:56.722 Checking status 7.
// 11:59:56.972 Checking status 8.
// 11:59:57.223 Checking status 9.
// 11:59:57.473 Checking status 10.
//
// Changing period to .5 seconds.
//
// 11:59:57.474 Checking status 1.
// 11:59:57.976 Checking status 2.
// 11:59:58.476 Checking status 3.
// 11:59:58.977 Checking status 4.
// 11:59:59.477 Checking status 5.
// 11:59:59.977 Checking status 6.
// 12:00:00.478 Checking status 7.
// 12:00:00.980 Checking status 8.
// 12:00:01.481 Checking status 9.
// 12:00:01.981 Checking status 10.
//
// Destroying timer.
.NET 包括四个类名为Timer
,每个的它提供了不同的功能:
- System.Timers.Timer它触发事件并执行的代码中一个或多个事件接收器按固定间隔。 类适用于作为基于服务器的使用或在多线程环境; 中的服务组件它没有用户界面并不是在运行时中可见。
- System.Threading.Timer其中按固定间隔在线程池线程上执行的单个回调方法。 当计时器实例化,并且不能更改定义的回调方法。 如System.Timers.Timer类,此类旨在为基于服务器或服务组件在多线程环境中使用; 它没有用户界面并不是在运行时中可见。
- System.Windows.Forms.Timer (仅在.NET framework 中),触发事件并在固定时间间隔的一个或多个事件接收器中执行代码的 Windows 窗体组件。 该组件没有用户界面,专供在单线程环境中;它在 UI 线程上执行。
- System.Web.UI.Timer (仅在.NET framework 中),按固定时间间隔执行异步或同步网页回发的 ASP.NET 组件。
c# 多线程之-- System.Threading Timer的使用的更多相关文章
- System.Threading.Timer的使用技巧
转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- .NET中System.Diagnostics.Stopwatch、System.Timers.Timer、System.Threading.Timer 的区别
1.System.Diagnostics.Stopwatch Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间. 在典型的 Stopwatch 方案中,先调用 ...
- 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 ...
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- C# System.Threading.Timer 使用方法
public class TimerHelper { System.Threading.Timer timer; public TaskSendMMS tasksendmms { get; set; ...
- System.Threading.Timer使用心得
System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...
- System.Threading.Timer 使用
//定义计时器执行完成后的回调函数 TimerCallback timecallback = new TimerCallback(WriteMsg); //定义计时器 System.Threading ...
- System.Threading.Timer如何正确地被Dispose
System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了 ...
随机推荐
- spring boot整合RabbitMQ(Topic模式)
1.Topic交换器介绍 Topic Exchange 转发消息主要是根据通配符. 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发 ...
- 如何虚拟机里安装Win8操作系统
不多说,直接上干货! Windows Server 2003.2008.2012系统的安装 推荐网址:打开MSDN网站(http://msdn.itellyou.cn ) 关于给电脑换系统,很多人会花 ...
- Selenium Web自动化 原理
文章转自 白月黑羽教Python 原理 说到web应用自动化测试,第一选择就是 Selenium 框架. Selenium 是一个 Web 应用的自动化框架. 通过它,我们可以写出自动化程序像人一样( ...
- Hadoop基础总结
一.Hadoop是什么? Hadoop是开源的分布式存储和分布式计算平台 二.Hadoop包含两个核心组成: 1.HDFS: 分布式文件系统,存储海量数据 a.基本概念 -块(block) HDFS的 ...
- 将AJAX Post的Data转为对应的Class
在使用DataTables从服务端获取数据时,在非MVC的情况下没有MVC的自动绑定功能,所以需要自己写一个绑定,将Post过来的InputStream转为对应的类. HTML: <form i ...
- Hive学习之Locking
众所周知,数据库必须要能够支持并发.无论在任何时候,允许同一时刻,多个用户能够同时读取或写入.没有必要给用户提供API显示的获取锁,所以所有的锁都是隐式获取的. 在Hive中有两种类型的锁: 共享锁S ...
- EMC,EMI,EMS,ESD分别是什么?有什么区别和联系?
一.EMC EMI EMS定义: EMC(ElectromagneticCompatibility) 电磁兼容,是指设备或系统在电磁环境中性能不降级的状态.电磁兼容,一方面要求系统内没有严重的干扰源, ...
- [转]SQL SERVER数据库删除LOG文件和清空日志的方案
本文转自:https://www.cnblogs.com/ShaYeBlog/archive/2012/09/04/2670505.html 数据库在使用过程中会使日志文件不断增加,使得数据库的性能下 ...
- api.execScript
在指定 window 或者 frame 中执行脚本,对于 frameGroup 里面的 frame 也有效,若 name 和 frameName 都未指定,则在当前 window 中执行脚本,具体执行 ...
- jQuery源码分析-03构造jQuery对象-源码结构和核心函数
3. 构造jQuery对象 3.1源码结构 先看看总体结构,再做分解: (function( window, undefined ) { var jQuery = (function() { // 构 ...