c# 多线程之-- System.Threading Timer的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# 多线程之-- System.Threading Timer的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3221字,纯文字阅读大概需要5分钟。
内容图文
![c# 多线程之-- System.Threading Timer的使用](/upload/InfoBanner/zyjiaocheng/841/e4a11dc822464823b826c79f6185fd41.jpg)
作用:每隔多久去执行线程里的方法.
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(10); // 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, 1000, 250); // When autoEvent signals, change the period to every half second. autoEvent.WaitOne(); stateTimer.Change(0, 500); Console.WriteLine("\nChanging period to .5 seconds.\n"); // When autoEvent signals the second time, dispose of the timer. autoEvent.WaitOne(); stateTimer.Change(0, 1000); 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 = 0; 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 = 0; 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的使用全部内容,希望文章能够帮你解决c# 多线程之-- System.Threading Timer的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。