2012-04-18 9 views
6

Mam działającą usługę Windows, w tym miejscu chcę uruchomić funkcję co minutę. Znalazłem kod, ale wygląda na to, że nie działa? Mam rejestrator i nie wydaje się, że kiedykolwiek wchodzi w funkcję timer_Elapsed?jak uruchomić funkcję w serwisie co 10 minut?

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(); 
     } 
    } 
+1

Czy potrzebujesz wywołać start w Timer? –

+0

zobacz http://stackoverflow.com/questions/246697/windows-service-and-timer –

+0

ahhh yeeeeeeeee – Beginner

Odpowiedz

16

Trzeba uruchomić stoper:

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 
} 
+4

Przez 10 minut wolę używać 'TimeSpan.FromMinutes (10) .TotalMilliseconds'' zamiast '10 * 60 * 1000' – Jaider

3

nie widzę _timer.Start(), że powinno być problemu.

1

Spróbuj rozpoczęciem 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(); 
     } 
    } 
2

Daniel Hilgarth jest poprawna - głównym problemem jest to, że nigdy nie zadzwonisz Start na zegar.

Mimo to warto rozważyć użycie Harmonogramu zadań systemu Windows zamiast usługi z licznikiem czasu. Pozwala to zaplanować uruchamianie zadania co 10 minut, ale także zmienić harmonogram w razie potrzeby bez zmiany kompilacji.

3

Też potrzebuję tej funkcjonalności. Oznacza to, że moja usługa Windows C# musi sprawdzać pocztę co 10 minut. I okrojona pewnej logiki, aby bardziej efektywny kod w następujący sposób:

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(); 
     } 

Sposób timer_elapsed rzeczywiście będzie dzwonić co 10 minut, począwszy od pierwszego _timer.start(), która pominięcia go przy okazji . Nie zrobiłem żadnego sprawdzenia _lastRun i startAt. Nie sądzę, żebyśmy tego potrzebowali

Powiązane problemy