ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemthreadingtimerclasstopic.htm================
using System;
using System.Threading;class TimerExampleState
{
public int counter = 0;
public Timer tmr;
}class App
{
public static void Main()
{
TimerExampleState s = new TimerExampleState(); // Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate = new TimerCallback(CheckStatus); // Create a timer that waits one second, then invokes every second.
Timer timer = new Timer(timerDelegate, s,1000, 1000);
// Keep a handle to the timer, so it can be disposed.
s.tmr = timer; // The main thread does nothing until the timer is disposed.
while(s.tmr != null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
}
// The following method is called by the timer's delegate. static void CheckStatus(Object state)
{
TimerExampleState s =(TimerExampleState)state;
s.counter++;
Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);
if(s.counter == 5)
{
// Shorten the period. Wait 10 seconds to restart the timer.
(s.tmr).Change(10000,100);
Console.WriteLine("changed...");
}
if(s.counter == 10)
{
Console.WriteLine("disposing of timer...");
s.tmr.Dispose();
s.tmr = null;
}
}
}
using System;
using System.Threading;class TimerExampleState
{
public int counter = 0;
public Timer tmr;
}class App
{
public static void Main()
{
TimerExampleState s = new TimerExampleState(); // Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate = new TimerCallback(CheckStatus); // Create a timer that waits one second, then invokes every second.
Timer timer = new Timer(timerDelegate, s,1000, 1000);
// Keep a handle to the timer, so it can be disposed.
s.tmr = timer; // The main thread does nothing until the timer is disposed.
while(s.tmr != null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
}
// The following method is called by the timer's delegate. static void CheckStatus(Object state)
{
TimerExampleState s =(TimerExampleState)state;
s.counter++;
Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);
if(s.counter == 5)
{
// Shorten the period. Wait 10 seconds to restart the timer.
(s.tmr).Change(10000,100);
Console.WriteLine("changed...");
}
if(s.counter == 10)
{
Console.WriteLine("disposing of timer...");
s.tmr.Dispose();
s.tmr = null;
}
}
}
解决方案 »
- WCF服务无法启动的问题
- c# 自动备份oracle数据
- 如何实现将类的属性、方法等点出来的功能
- Pocket PC智能设备项目,无法找到PInvoke DLL问题
- 为什么picturebox上的label有背景色?
- "#"符号是什么意思?
- 新手提问:如何单击datagrid绑定列弹出新页面?
- 有关提示框的
- 单色的背景上有一个自定义不规则图形,如何根据这个不规则图形的形状生成一个GraphicsPath或region.
- VisualSVN for Visual Studio 2019 总是导致Visual Studio 无响应
- 今天来结贴,谢谢csdn的朋友,也带来一个问题 :)
- 怎么判断一个指的路径下是否存在某个文件?
System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。 推荐使用System.Timers.Timer,代码这个地方有,自己读读就好.
ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemtimerstimerclasstopic.htm还有,如果要up的话,最好能说说那个地方不明白,或者没有说清楚.
基于服务器的计时器允许指定在应用程序中引发事件的重复时间间隔。然后可通过处理这个事件来提供常规处理。例如,假设您有一台关键的服务器,必须保持一周 7 天、一天 24 小时连续运行。您可以创建一个服务,通过使用计时器来定期检查关键的服务器,确保系统启动并运行。如果该系统没有响应,此服务可以尝试重新启动服务器或通知系统管理员。注意 以毫秒为单位指定服务器计时器的时间间隔。
服务器计时器、Windows 计时器和线程计时器
在 Visual Studio .NET 和 .NET Framework 中有三种计时器控件:基于服务器的计时器,位于“工具箱”的“组件”选项卡上;基于 Windows 的标准计时器,位于“工具箱”的“Windows 窗体”选项卡上,以及仅可在编程时使用的线程计时器。基于 Windows 的计时器从 Visual Basic 的 1.0 版起就存在于该产品中并且基本上保持不变。该计时器已经为在 Windows 窗体应用程序中使用而进行了优化。基于服务器的计时器是传统的计时器为了在服务器环境上运行而优化后的更新版本。线程计时器是一种简单的、轻量级计时器,使用回调方法而不是事件,并由线程池线程提供。在 Win32 体系结构中有两种类型的线程:UI 线程和辅助线程。UI 线程绝大多数时间处于空闲状态,等待消息循环中的消息到来。一旦接收到消息,它们就进行处理并等待下一个消息到来。另外,辅助线程用来执行后台处理而且不使用消息循环。Windows 计时器和基于服务器的计时器在运行时都使用 Interval 属性。线程计时器的时间间隔在 Timer 构造函数中设置。计时器的设计目的各不相同,它们的线程处理明确地指出了这一点: Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。Windows 计时器的精度限定为 55 毫秒。这些传统计时器要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。对于 COM 组件来说,这样会降低性能。
基于服务器的计时器是为在多线程环境下与辅助线程一起使用而设计的。由于它们使用不同的体系结构,因此基于服务器的计时器可能比 Windows 计时器精确得多。服务器计时器可以在线程之间移动来处理引发的事件。
对消息不在线程上发送的方案中,线程计时器是非常有用的。例如,基于 Windows 的计时器依赖于操作系统计时器的支持,如果不在线程上发送消息,与计时器相关的事件将不会发生。在这种情况下,线程计时器就非常有用。
Windows 计时器位于 System.Windows.Forms 命名空间中,服务器计时器位于 System.Timers 命名空间中,而线程计时器位于 System.Threading 命名空间中。请参见
计时器编程体系结构 | 创建基于服务器的计时器实例 | 监视基于服务器的计时器 | Windows 窗体 Timer 组件介绍 | Timer(线程计时器)| 线程池 --------------------------------------------------------------------------------向 Microsoft 发送有关此主题的反馈© Microsoft Corporation。保留所有权利。