提供以指定的时间间隔对线程池线程执行方法的机制

  1. using System;
  2. using System.Threading;
  3.  
  4. class TimerExample
  5. {
  6. static void Main()
  7. {
  8. // Create an AutoResetEvent to signal the timeout threshold in the
  9. // timer callback has been reached.
  10. var autoEvent = new AutoResetEvent(false);
  11.  
  12. var statusChecker = new StatusChecker();
  13.  
  14. // Create a timer that invokes CheckStatus after one second,
  15. // and every 1/4 second thereafter.
  16. Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
  17. DateTime.Now);
  18. var stateTimer = new Timer(statusChecker.CheckStatus,
  19. autoEvent, , );
  20.  
  21. // When autoEvent signals, change the period to every half second.
  22. autoEvent.WaitOne();
  23. stateTimer.Change(, );
  24. Console.WriteLine("\nChanging period to .5 seconds.\n");
  25.  
  26. // When autoEvent signals the second time, dispose of the timer.
  27. autoEvent.WaitOne();
  28. stateTimer.Dispose();
  29. Console.WriteLine("\nDestroying timer.");
  30. }
  31. }
  32.  
  33. class StatusChecker
  34. {
  35. private int invokeCount;
  36. private int maxCount;
  37.  
  38. public StatusChecker(int count)
  39. {
  40. invokeCount = ;
  41. maxCount = count;
  42. }
  43.  
  44. // This method is called by the timer delegate.
  45. public void CheckStatus(Object stateInfo)
  46. {
  47. AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
  48. Console.WriteLine("{0} Checking status {1,2}.",
  49. DateTime.Now.ToString("h:mm:ss.fff"),
  50. (++invokeCount).ToString());
  51.  
  52. if(invokeCount == maxCount)
  53. {
  54. // Reset the counter and signal the waiting thread.
  55. invokeCount = ;
  56. autoEvent.Set();
  57. }
  58. }
  59. }
  60. // The example displays output like the following:
  61. // 11:59:54.202 Creating timer.
  62. //
  63. // 11:59:55.217 Checking status 1.
  64. // 11:59:55.466 Checking status 2.
  65. // 11:59:55.716 Checking status 3.
  66. // 11:59:55.968 Checking status 4.
  67. // 11:59:56.218 Checking status 5.
  68. // 11:59:56.470 Checking status 6.
  69. // 11:59:56.722 Checking status 7.
  70. // 11:59:56.972 Checking status 8.
  71. // 11:59:57.223 Checking status 9.
  72. // 11:59:57.473 Checking status 10.
  73. //
  74. // Changing period to .5 seconds.
  75. //
  76. // 11:59:57.474 Checking status 1.
  77. // 11:59:57.976 Checking status 2.
  78. // 11:59:58.476 Checking status 3.
  79. // 11:59:58.977 Checking status 4.
  80. // 11:59:59.477 Checking status 5.
  81. // 11:59:59.977 Checking status 6.
  82. // 12:00:00.478 Checking status 7.
  83. // 12:00:00.980 Checking status 8.
  84. // 12:00:01.481 Checking status 9.
  85. // 12:00:01.981 Checking status 10.
  86. //
  87. // Destroying timer.
  1. using System;
  2. using System.Threading;
  3.  
  4. class TimerExample
  5. {
  6. public static int loop_index = ;
  7. public static AutoResetEvent autoEvent = new AutoResetEvent(false);
  8.  
  9. static void Main()
  10. {
  11. int loop_count = ;
  12.  
  13. // Create an AutoResetEvent to signal the timeout threshold in the
  14. // timer callback has been reached.
  15.  
  16. // Create a timer that invokes CheckStatus after one second,
  17. // and every 1/4 second thereafter.
  18. Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
  19. DateTime.Now);
  20. var stateTimer = new Timer(show,
  21. autoEvent, , );
  22.  
  23. // When autoEvent signals, change the period to every half second.
  24. autoEvent.WaitOne();
  25. stateTimer.Change(, );
  26. Console.WriteLine("\nChanging period to .5 seconds.\n");
  27.  
  28. // When autoEvent signals the second time, dispose of the timer.
  29. autoEvent.WaitOne();
  30. stateTimer.Dispose();
  31. Console.WriteLine("\nDestroying timer.");
  32. }
  33.  
  34. public static void show(Object stateInfo)
  35. {
  36. Console.WriteLine("{0:h:mm:ss.fff} Run in Show.\n",
  37. DateTime.Now);
  38. Console.WriteLine(loop_index);
  39. loop_index = loop_index + ;
  40. if (loop_index > )
  41. {
  42. autoEvent.Set();
  43. }
  44. }
  45. }

构造函数

Timer(TimerCallback)

使用新创建的 Timer 对象作为状态对象,用一个无限周期和一个无限到期时间初始化Timer 类的新实例。

Timer(TimerCallback, Object, Int32, Int32)

使用 32 位的有符号整数指定时间间隔,初始化 Timer 类的新实例。

Timer(TimerCallback, Object, Int64, Int64)

用 64 位有符号整数来度量时间间隔,以初始化 Timer 类的新实例。

Timer(TimerCallback, Object, TimeSpan, TimeSpan)

初始化 Timer 类的新实例,使用 TimeSpan 值来度量时间间隔。

Timer(TimerCallback, Object, UInt32, UInt32)

用 32 位无符号整数来度量时间间隔,以初始化 Timer 类的新实例。

方法

Change(Int32, Int32)

更改计时器的启动时间和方法调用之间的间隔,用 32 位有符号整数度量时间间隔。

Change(Int64, Int64)

更改计时器的启动时间和方法调用之间的间隔,用 64 位有符号整数度量时间间隔。

Change(TimeSpan, TimeSpan)

更改计时器的启动时间和方法调用之间的时间间隔,使用 TimeSpan 值度量时间间隔。

Change(UInt32, UInt32)

更改计时器的启动时间和方法调用之间的间隔,用 32 位无符号整数度量时间间隔。

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(Inherited from MarshalByRefObject)

Dispose()

释放由 Timer 的当前实例使用的所有资源。

Dispose(WaitHandle)

释放 Timer 的当前实例使用的所有资源并在释放完计时器时发出信号。

Equals(Object)

确定指定的对象是否等于当前对象。

(Inherited from Object)

GetHashCode()

作为默认哈希函数。

(Inherited from Object)

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。

(Inherited from MarshalByRefObject)

GetType()

获取当前实例的 Type

(Inherited from Object)

InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。

(Inherited from MarshalByRefObject)

MemberwiseClone()

创建当前 Object 的浅表副本。

(Inherited from Object)

MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(Inherited from MarshalByRefObject)

ToString()

返回表示当前对象的字符串。

(Inherited from Object)

C# System.Threading.Timer的更多相关文章

  1. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

  2. C# System.Threading.Timer 使用方法

    public class TimerHelper { System.Threading.Timer timer; public TaskSendMMS tasksendmms { get; set; ...

  3. System.Threading.Timer使用心得

    System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...

  4. System.Threading.Timer 使用

    //定义计时器执行完成后的回调函数 TimerCallback timecallback = new TimerCallback(WriteMsg); //定义计时器 System.Threading ...

  5. System.Threading.Timer的使用技巧

    转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...

  6. System.Threading.Timer如何正确地被Dispose

    System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了 ...

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

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

  8. System.Threading.Timer

    GLog.WLog("_thdTimer before"); _thdTimer = new System.Threading.Timer(new TimerCallback(Ti ...

  9. 当时钟事件声明为过程变量 让system.threading.timer时钟失效

    这个项目的小模块就是画label 控件到tablepayoutpanel表单 之中, 中间用到了时钟,事件(带返回值的),哈希表 .由于时钟定义在 form1的启动构造函数中导致了form1,启动完毕 ...

  10. c# 多线程之-- System.Threading Timer的使用

    作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...

随机推荐

  1. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第九集-补充-之安装jdk】

    1,安装JDK,本来想安装jdk8的,但是考虑到tomcat安装的是tomcat7,怕出现版本不兼容的情况,就改安装jdk7 去官网下载jdk-7u80-linux-x64.tar.gz的二进制文件, ...

  2. Java03动手动脑

    1.当JAVA里定义的函数中去掉static后,怎么办? static代表静态,由于main函数是静态的,如果自己定义的函数方法加了static则在类加载时就一起加载了.但如果不写static,就必须 ...

  3. 039 在weblogic下部署jndi的多数据源

    这个问题,在公司遇到了,一直没有学,今天学了一下. 后续,还要实验一下,暂时粘贴一下一个不错的url:https://www.cnblogs.com/xdp-gacl/p/4201094.html

  4. sql语句start with connect by prior语法解析

    prior分两种放法: 1 放在子节点端 表示start with 指定的节点作为根节点,按照从上到下的顺序遍历 2 放在父节点端 表示start with指定的节点作为最底层节点,按照从下到上的顺序 ...

  5. 【python】进程与线程

    No1: 多进程 from multiprocessing import Process import os # 子进程要执行的代码 def run_proc(name): print('Run ch ...

  6. 深度学习中的batch_size,iterations,epochs等概念的理解

    在自己完成的几个有关深度学习的Demo中,几乎都出现了batch_size,iterations,epochs这些字眼,刚开始我也没在意,觉得Demo能运行就OK了,但随着学习的深入,我就觉得不弄懂这 ...

  7. oracle 重复只保留一条

    DELETE FROM xx WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM xx  GROUP BY xx, xx);

  8. elasticsearch数据备份还原

    elasticsearch数据备份还原 1.在浏览器中运行http://XXX.XXX.XXX.XXX:9200/_flush,确保索引数据能保存到硬盘中. 2.原数据的备份.主要是elasticse ...

  9. SpringBoot的第一个web项目

    这一节主要是讲springboot搭建简单的web项目. 首先pom文件新增spring-boot-starter-web依赖,pom文件如下所示 <?xml version="1.0 ...

  10. XamarinAndroid组件教程RecylerView适配器设置动画

    XamarinAndroid组件教程RecylerView适配器设置动画 本小节将讲解动画相关设置,如动画的时长.插值器以及复合动画等. 1.设置动画时长 设置动画持续的时间可以使用Animation ...