How to perform a function inside a service every 10 minutes?

I have a Windows service running inside which I want to run a function every minute. I found the code, but it does not work? I have a logger and it does not seem to go into the timer_Elapsed function?

protected override void OnStart(string[] args) { // SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient(); // test.Import(); log.Info("Info - Service Started"); _timer = new Timer(10 * 60 * 1000); // every 10 minutes?? _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { log.Info("Info - Check time"); DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48); if (_lastRun < startAt && DateTime.Now >= startAt) { // stop the timer _timer.Stop(); try { log.Info("Info - Import"); SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient(); test.Import(); } catch (Exception ex) { log.Error("This is my error - ", ex); } _lastRun = DateTime.Now; _timer.Start(); } } 
+6
source share
5 answers

You need to start the timer:

 protected override void OnStart(string[] args) { log.Info("Info - Service Started"); _timer = new Timer(10 * 60 * 1000); // every 10 minutes _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); _timer.Start(); // <- important } 
+17
source

I do not see _timer.Start() , this should be your problem.

+3
source

I also need this functionality. That is, my Windows C # service should check email every 10 minutes. I shared some logic to make the code more efficient, as follows:

 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { _timer.Stop(); try { EventLog.WriteEntry(Program.EventLogName, "Checking emails " + _count++); } catch (Exception ex) { EventLog.WriteEntry(Program.EventLogName, "This is my error " + ex.Message); } _timer.Start(); } 

The timer_elapsed method will actually be called every 10 minutes, starting with the first _timer.start (), which you, by the way, will miss. I did not check _lastRun and startAt. I don't think we need it

+3
source

Daniel Hilgart is right - the main problem is that you never call Start on the timer.

However, you may also consider using Windows Task Scheduler instead of a timer service. This allows you to schedule the task to run every 10 minutes, but also change the schedule if necessary without changing the compilation.

+2
source

Try to start the timer ,

 protected override void OnStart(string[] args) { // SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient(); // test.Import(); log.Info("Info - Service Started"); _timer = new Timer(10 * 60 * 1000); // every 10 minutes?? _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); _timer.Start(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { log.Info("Info - Check time"); DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48); if (_lastRun < startAt && DateTime.Now >= startAt) { // stop the timer _timer.Stop(); try { log.Info("Info - Import"); SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient(); test.Import(); } catch (Exception ex) { log.Error("This is my error - ", ex); } _lastRun = DateTime.Now; _timer.Start(); } } 
+1
source

Source: https://habr.com/ru/post/913505/


All Articles