c USES timer instance code in the windows service

  • 2020-05-27 07:00:00
  • OfStack

Since I have been doing the automatic program recently, I started to do the windows service program.

How to use timer in windows service when 1 fails,

Before is directly drags into timer control, but cannot directly run, found 1 paragraph procedure on the net later, good make.


// Start event 
        protected override void OnStart(string[] args)
        {
             // Timing of events 
            MyTimer(); 
        }
        // The end of the event 
        protected override void OnStop()
        {
            writeLog(" Service end time: "  + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        // instantiation System.Timers.Timer   
        private void MyTimer()
        {
            System.Timers.Timer MT = new System.Timers.Timer(30000);
            MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);
            MT.Enabled = true;
        }
        // structure System.Timers.Timer The instance     Interval time event    
        private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            // Implementation method 

        }


Related articles: