// 摘要:
// 在应用程序中生成定期事件。
[DefaultEvent("Elapsed")]
[DefaultProperty("Interval")]
public class Timer : Component, ISupportInitialize
static class Program
{
private static System.Timers.Timer aTimer;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
MessageBox.Show(null,"test","test");
}
}c# Timer_应用程序中生成定期事件
最新推荐文章于 2026-02-24 17:03:04 发布
本文介绍了一个简单的C#程序,该程序使用System.Timers命名空间下的Timer类来创建一个每5秒触发一次事件的应用。通过设置定时器的时间间隔并定义事件处理程序,演示了如何响应定时触发的事件。

2556

被折叠的 条评论
为什么被折叠?



