【发布时间】:2016-05-25 14:31:52
【问题描述】:
我有一个运行 2 个计时器的 Windows 服务,一个以 15 分钟为间隔,一个以 24 小时为间隔。 我添加了事件日志记录以检查经过的事件处理程序是否在 15 分钟触发,但似乎没有。任何人都可以看到这段代码有什么问题吗?
public partial class GTstaging : ServiceBase
{
System.Timers.Timer regularTimer = new System.Timers.Timer();
System.Timers.Timer longTimer = new System.Timers.Timer();
DateTime _scheduleTime;
public staging()
{
InitializeComponent();
_scheduleTime = DateTime.Today.AddDays(1).AddHours(Convert.ToDouble(ConfigurationManager.AppSettings["ScheduleTime_" + DateTime.Now.DayOfWeek.ToString()]));
}
protected override void OnStart(string[] args)
{
//** original - ConsoleApplication1.Program.DoProcessing();
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry("START, regular timer:" + ConfigurationManager.AppSettings["RegularTimer"], EventLogEntryType.Information, 101, 1);
}
this.regularTimer.Enabled = true;
this.regularTimer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["RegularTimer"]);
this.regularTimer.AutoReset = true;
this.regularTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoRegular);
this.longTimer.Enabled = true;
this.longTimer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
this.longTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoLongRunning);
}
private void DoRegular(object sender, System.Timers.ElapsedEventArgs e)
{
//do stuff then log
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry("Regular Process End", EventLogEntryType.Information, 101, 1);
}
}
private void DoLongRunning(object sender, System.Timers.ElapsedEventArgs e)
{
//do stuff
}
protected override void OnStop()
{
this.regularTimer.Stop();
this.longTimer.Stop();
this.regularTimer = null;
this.longTimer = null;
}
}
}
【问题讨论】:
-
ConfigurationManager.AppSettings["RegularTimer"]写入eventLog时的设置是什么? -
我想知道您是否尝试写入
DoRegular事件处理程序中的文件而不是事件日志。我认为您正在两个不同的线程(服务的主线程和计时器事件处理程序)中访问日志。因此,您的事件很可能正在触发,但由于日志记录,您正在查看一个误报。 Check this MSDN page
标签: c# timer windows-services