hemtros
11/20/2015 - 6:19 PM

To rerun a piece of code every X milliseconds in windows service

To rerun a piece of code every X milliseconds in windows service

public partial class MyClass : ServiceBase
    {
      System.Timers.Timer timer = new System.Timers.Timer();

        public DSTManager()
        {
            InitializeComponent();
        }
        
        //This method fires when windows service starts
        protected override void OnStart(string[] args)
        {
          timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Interval = 86400000;
            //timer.Interval = 90000;
            timer.Enabled = true;
        }
        
        //This method fires when windows service stops
        protected override void OnStop()
        {
          timer.Enabled = false;
        }
        
        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            //this code runs every x milliseconds
        }
    }