Windows 服务在首次成功运行后未启动
本文关键字:运行 启动 成功 服务 Windows | 更新日期: 2023-09-27 18:32:37
我创建了一个Windows服务,该服务将根据某些条件向用户发送邮件。我以自动模式将其安装在服务器上。从日志中,我可以看到它第一次成功运行并结束。
在那之后,我没有看到它在日志中再次运行。
我在管理工具中检查了该服务,它说它已启动。
我也重新启动了服务,但没有使用,它没有再次启动。
下面是我用来启动服务的代码。
public partial class ScheduledService : ServiceBase
{
Timer timer;
private DateTime lastRun = DateTime.Now;
private DateTime DailyRunTime = Convert.ToDateTime(System.Configuration.ConfigurationManager.AppSettings["DailyRunTime"]);
public ScheduledService()
{
InitializeComponent();
//GetDocRetentionList DocList = new GetDocRetentionList();
//DocList.GetDataSet();
}
protected override void OnStart(string[] args)
{
//System.Diagnostics.Debugger.Launch();
TraceService("start service");
//timer = new Timer(24 * 60 * 60 * 1000);
timer = new Timer(10 * 60 * 1000);
timer.Start();
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
double TimerInterval = Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["Timer"]);
timer.Interval = TimerInterval;
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
TraceService("stopping service");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
TraceService("Service started at " + DateTime.Now);
if (lastRun.Date < DateTime.Now.Date)
{
if (DateTime.Now > DailyRunTime)
{
GetDocRetentionList DocList = new GetDocRetentionList();
DocList.GetDataSet();
timer.Stop();
lastRun = DateTime.Now.Date;
//timer.Start();
}
}
}
我在这方面能得到的任何帮助都会非常有帮助。请让我知道。
好吧,您的服务设置为执行一次,然后它会在 OnElapsedTime
方法中关闭计时器,但永远不会重新打开自身。
OnElapsedTime
应该做的第一件事是关闭计时器。 它应该做的最后一件事就是重新打开它。