using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadTimerExam
{
class Program
{
static void Main(string[] args)
{
var timer = new System.Threading.Timer(TimerAction, null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3));
Thread.Sleep(15000);
timer.Dispose();
Console.Read();
}
static void TimerAction(object obj)
{
Console.WriteLine("System.Threading.Timer {0:T}", DateTime.Now);
}
}
}

本文介绍如何使用C#中的System.Threading.Timer类来创建一个定时任务。该任务每隔三秒执行一次,持续运行15秒后停止。示例代码展示了如何设置及启动定时器,并在指定的时间间隔内重复执行特定的操作。


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



