我创建了一个 Windows 服务来运行一段代码,并在其中实现了计时器以定期运行它。
我的计时器课程是:
class TimerClass
{
private static System.Timers.Timer aTimer;
public static void Main()
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000;
aTimer.Enabled = true;
GC.KeepAlive(aTimer);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
aTimer.Stop();
DatabaseUpdation dbUp = new DatabaseUpdation();
File.AppendAllText(@"C:\Documents and Settings\New Folder\My Documents\demo\abc.txt", "Start" + " " + DateTime.Now.ToString() + Environment.NewLine);
dbUp.GetDatafromSource();
aTimer.Start();
}
}
我从我的 Start 方法中调用它:
protected override void OnStart(string[] args)
{
TimerClass timer = new TimerClass();
}
但是计时器根本没有执行。谁能在这里找到我的错误?提前致谢