服务器计时器与 Windows 计时器

    技术2022-05-11  23

    <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 说明:此文大部分内容摘自MSDN,本人只是做了一点整理。如果有什么异议,一切以MSDN为准。一、引言 在 Visual Studio .NET 中有两种计时器控件——基于服务器的计时器和标准的基于 Windows 的计时器。基于 Windows 的计时器为在 Windows 窗体应用程序中使用而进行了优化,基于服务器的计时器是传统的计时器为了在服务器环境上运行而优化后的更新版本。 二、两种不同的计时器有什么区别呢? 在 Win32 体系结构中有两种类型的线程:UI 线程和辅助线程。UI 线程绝大多数时间处于空闲状态,等待消息循环中的消息到来。一旦接收到消息,它们就进行处理并等待下一个消息到来。另外,辅助线程用来执行后台处理而且不使用消息循环。虽然 Windows 计时器和基于服务器的计时器运行时都使用 Interval 属性,但它们的设计用途是不同的(这一点可由它们对线程的处理方式来证明): Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。 Windows 计时器的精度限定为 55 毫秒。这些传统计时器要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。对于 COM 组件来说,这样会降低性能。 基于服务器的计时器是为在多线程环境下与辅助线程一起使用而设计的。由于它们使用不同的体系结构,因此基于服务器的计时器可能比 Windows 计时器精确得多。服务器计时器可以在线程之间移动来处理引发的事件。 基于服务器的 Timer 是为在多线程环境中用于辅助线程而设计的。服务器计时器可以在线程间移动来处理引发的 Elapsed 事件,这样就可以比 Windows 计时器更精确地按时引发事件。有关基于服务器的计时器的更多信息,请参阅基于服务器的计时器介绍。三、使用示例这里给出了两种计时器的示例,以便大家参考使用。1、服务器计时器使用示例//using System.Timers; public class Timer1 { public static void Main() { System.Timers.Timer aTimer = new System.Timers.Timer(); //设置事件处理句柄,此处为OnTimedEvent方法: aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); aTimer.Interval=5000;//设置间隔时间为5秒 aTimer.AutoReset=true; /* *注意 当 AutoReset 设置为 false 时,Timer 只在第一个 Interval *过后引发一次 Elapsed 事件。若要保持以 Interval 时间间隔引发 *Elapsed 事件,请将 AutoReset 设置为 true。 */ aTimer.Enabled=true;//计时开始,此处也可以用aTimer.Start(); Console.WriteLine("Press 'q' to quit the sample."); while(Console.Read()!='q'); aTimer.Enabled=false;//计时结束,此处也可以用aTimer.Stop(); } //这里就是 Elapsed 事件发生时所调用的方法: public static void OnTimedEvent(object source, ElapsedEventArgs e) { Console.WriteLine("Hello World!"); } }2、Windows 计时器使用示例 Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。它要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。//using System.Windows.Forms; public class Class1 { static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); static int alarmCounter = 1; static bool exitFlag = false; // 在时间到达的时候就运行下面的方法: private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.Stop(); // 显示一个对话框,询问是否继续运行计时器 if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes) { // 重新启动计时器 alarmCounter +=1; myTimer.Enabled = true; } else { //关闭计时器 exitFlag = true; } } public static int Main() { /* 设置事件处理句柄 ,这里的Tick与服务器计时其中的Elapsed相对应*/ myTimer.Tick += new EventHandler(TimerEventProcessor); myTimer.Interval = 5000;// 初始化计时器 myTimer.Start(); // 开始运行计时器 while(exitFlag == false) { //延迟一段时间 Application.DoEvents(); } return 0; } } <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

    最新回复(0)