using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace TimerApplication
{
class Program
{
static void Main(string[] args)
{
Timer timer = new Timer(5*1000);
timer.Elapsed += new ElapsedEventHandler(outMessage);
timer.AutoReset = true;
timer.Enabled = true;
string strLine;
do
{
strLine = Console.ReadLine();
} while (strLine != null && strLine != "exit");
}
public void outMessage(object source,ElapsedEventArgs e)
{
Console.WriteLine("Hello");
}
}
}
本文介绍如何使用C#中的System.Timers.Timer类来创建一个简单的定时应用。该应用每5秒输出一条消息,并通过控制台接收输入指令来退出程序。文章展示了如何设置定时器的间隔、触发事件处理程序以及如何启用和禁用定时器。
9989

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



