2013-09-23 41 views
-1

我创建了一个windows服务,它将根据某些条件向用户发送邮件。 我在自动模式下将它安装在服务器上。从日志中我可以看到它第一次成功运行并结束。第一次成功运行后,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(); 
      } 
     } 

    } 

任何帮助,我可以在这方面得到的将是非常有益的。 Plz让我知道。

回答

2

那么..你的服务设置为执行一次,然后关闭定时器在OnElapsedTime方法,但永远不会重新打开。

OnElapsedTime应该做的第一件事是关闭定时器。它应该做的最后一件事是重新开启。

+0

非常感谢克里斯。这应该解决我现在正在测试它的问题 – Kumar

相关问题