C# 无法保持计时器处于活动状态的windows服务

C# 无法保持计时器处于活动状态的windows服务,c#,timer,windows-services,C#,Timer,Windows Services,我有一个为我的服务器制作的windows服务 我需要每分钟检查我的SQL数据库中是否有一些新信息 所以我做了一个windows服务,创建了一个间隔为1分钟的计时器。 但是windows服务设置了计时器并结束了运行 事情是这样的: 开始服务 设置带间隔的计时器 完成和退出服务设置计时器(i); } } 警报计时器: 公共类警报计时器 { 静态System.Timers.Timer aTimer=新的System.Timers.Timer(); 公共警报计时器(TimerModel Ti

我有一个为我的服务器制作的windows服务

我需要每分钟检查我的SQL数据库中是否有一些新信息

所以我做了一个windows服务,创建了一个间隔为1分钟的计时器。 但是windows服务设置了计时器并结束了运行

事情是这样的:

    • 开始服务
    • 设置带间隔的计时器
    • 完成和退出服务设置计时器(i); } } 警报计时器:

      公共类警报计时器
      {
      静态System.Timers.Timer aTimer=新的System.Timers.Timer();
      公共警报计时器(TimerModel TimerModel,整数毫秒)
      {
      aTimer.Appead+=新的ElapsedEventHandler((发送方,e)=>OnTimedEvent(发送方,e,timerModel));
      时间间隔=毫秒;
      }    
      公开作废开始()
      {
      aTimer.Enabled=true;
      }
      timedevent上的公共静态void(对象源、ElapsedEventArgs e、TimerModel TimerModel)
      {
      
      GetAbscenceContacts.Start();您实际上并没有启动服务。您正在调用名为Start的方法,该方法不属于Windows服务类层次结构的一部分,它只是您定义的一个方法。您的方法运行并完成,因此服务退出

      试试这个:

      static void Main(string[] args)
          {
              try
              {
                  Utils.SetConfigFile();
                  ServiceBase[] ServicesToRun;
                  ServicesToRun = new ServiceBase[] 
                  { 
                      new TaoTimer() 
                  };
                  ServiceBase.Run(ServicesToRun);
              }
              catch (Exception ex)
              {
                  EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
              }
           }
      
      public partial class TaoTimer : ServiceBase
      {
          ...
          protected override void OnStart(string[] args)
          {
              SetTimerList();
              EventLog.WriteEntry("Started");
          }
          ....
      }
      

      并从TaoTimer中完全删除Start方法。

      您需要将您的
      AlertTimer
      实例存储在将持续服务生命周期的某个对象中(例如,在
      列表中,计时器本身不会阻止其被垃圾收集。示例说明:

          // Normally, the timer is declared at the class level, 
          // so that it stays in scope as long as it is needed. 
          // If the timer is declared in a long-running method,   
          // KeepAlive must be used to prevent the JIT compiler  
          // from allowing aggressive garbage collection to occur  
          // before the method ends. You can experiment with this 
          // by commenting out the class-level declaration and  
          // uncommenting the declaration below; then uncomment 
          // the GC.KeepAlive(aTimer) at the end of the method. 
          //System.Timers.Timer aTimer; 
      

      现在,虽然您的计时器是在
      AlertTimer
      类中的类级别声明的,但是没有任何东西可以阻止
      AlertTimer
      实例本身被收集。GC只会保持可传递访问的活动。一旦
      AlertTimer
      实例可收集,您的
      计时器
      对象。

      检查Windows事件日志,查看阻止其运行的未处理异常。@MichaelPerrenoud已经完成了…没有未处理的异常…是否启动了
      事件日志?是的,我将更新我的问题。.您提到过可以看到“服务已成功启动”,但您是否正在获取一开始就有的事件日志?
      
      public class AlertTimer
      {
          static System.Timers.Timer aTimer = new System.Timers.Timer();
          public AlertTimer(TimerModel timerModel, int milliseconds)
          {
              aTimer.Elapsed += new ElapsedEventHandler((sender, e) => OnTimedEvent(sender, e, timerModel));
              aTimer.Interval = milliseconds;
          }    
          public void Start()
          {
              aTimer.Enabled = true;
          }
          public static void OnTimedEvent(object source, ElapsedEventArgs e, TimerModel timerModel)
          {
              getAbsenceContacts.Start();<-- NEVER GETS HERE....
          }
      }
      
      static void Main(string[] args)
          {
              try
              {
                  Utils.SetConfigFile();
                  ServiceBase[] ServicesToRun;
                  ServicesToRun = new ServiceBase[] 
                  { 
                      new TaoTimer() 
                  };
                  ServiceBase.Run(ServicesToRun);
              }
              catch (Exception ex)
              {
                  EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
              }
           }
      
      public partial class TaoTimer : ServiceBase
      {
          ...
          protected override void OnStart(string[] args)
          {
              SetTimerList();
              EventLog.WriteEntry("Started");
          }
          ....
      }
      
          // Normally, the timer is declared at the class level, 
          // so that it stays in scope as long as it is needed. 
          // If the timer is declared in a long-running method,   
          // KeepAlive must be used to prevent the JIT compiler  
          // from allowing aggressive garbage collection to occur  
          // before the method ends. You can experiment with this 
          // by commenting out the class-level declaration and  
          // uncommenting the declaration below; then uncomment 
          // the GC.KeepAlive(aTimer) at the end of the method. 
          //System.Timers.Timer aTimer;