2017-09-15 3 views
0

J'ai une question de service Windows. Je suis en train d'écrire un service avec minuterie qui va prendre un certain temps et répéter continuellement.Réécriture du service Windows C uniquement après la fin d'une méthode

Dans mon code, je souhaite que la méthode timer1_Tick ne s'exécute qu'après la fin et non après toutes les 2 secondes. Comment puis-je y arriver? Des pointeurs?

Voici comment mon code ressemble:

using System; 
using System.ServiceProcess; 
using System.Timers; 

namespace TestWindowsService 
{ 
    public partial class Scheduler : ServiceBase 
    { 
     private Timer timer1 = null; 
     public Scheduler() 
     { 
      InitializeComponent(); 
     } 

     public void onDebug() 
     { 
      OnStart(null); 
     } 

     protected override void OnStart(string[] args) 
     { 

      timer1 = new Timer(); 
      this.timer1.Interval = 2000; //every 2 seconds 
      this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick); 
      timer1.Enabled = true; 
      Helper.ServiceStartLog("Test window service started"); 
     } 

     private void timer1_Tick (object sender, ElapsedEventArgs e) 
     { 
      PDFHelper.WriteErrorLog("Timer ticker and Log Running Job is Running"); 
     } 

     protected override void OnStop() 
     { 
      timer1.Enabled = false; 
      Helper.WriteErrorLog("Test window service stopped"); 
     } 
    } 
} 

Program.cs ressemble:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace TestWindowsService 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     static void Main() 
     { 

#if DEBUG 
      Scheduler myservice = new Scheduler(); 
      myservice.onDebug(); 
      Thread.Sleep(Timeout.Infinite); 

#else 
      ServiceBase[] ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new Scheduler() 
      }; 
      ServiceBase.Run(ServicesToRun); 
#endif 
     } 
    } 
} 

Classe d'assistance On dirait

using System; 
using System.IO; 
using System.Threading; 

namespace TestWindowsService 
{ 
    public static class Helper 
    { 

     public static void ServiceStartLog(string Message) 
    { 
     try 
     { 
      StreamWriter sw = null; 
      sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\logFile.txt", true); 
      sw.WriteLine(DateTime.Now.ToString() + ": " + Message); 
      sw.Flush(); 
      sw.Close(); 
     } 

     catch (Exception ex1) 
     { 
      Console.WriteLine(ex1.Message.ToString()); 

     } 
    } 

     public static void WriteErrorLog(string Message) 
     { 
      try 
      { 
       StreamWriter sw = null; 
       Thread.Sleep(5000); 
       sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\logFile.txt", true); 
       sw.WriteLine(DateTime.Now.ToString() + ": " + Message); 
       sw.Flush(); 
       sw.Close(); 
      } 

      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message.ToString()); 
      } 
     } 

    } 
} 

Répondre

1

Définissez la propriété AutoReset de la minuterie false, cela fera que la minuterie ne fonctionnera qu'une fois, puis à la fin de votre fonction de tick, vous redémarrerez le tim er.

protected override void OnStart(string[] args) 
{ 

    timer1 = new Timer(); 
    this.timer1.Interval = 2000; //every 2 seconds 
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick); 
    timer1.AutoReset = false; 
    timer1.Enabled = true; 
    Helper.ServiceStartLog("Test window service started"); 
} 

private void timer1_Tick (object sender, ElapsedEventArgs e) 
{ 
    try 
    { 
     PDFHelper.WriteErrorLog("Timer ticker and Log Running Job is Running"); 
    } 
    finally 
    { 
     //this gets run even if there was a exception. 
     timer1.Start(); 
    } 
} 
+0

oh cool. Laissez-moi essayer votre solution. – ProgSky

+0

fonctionne comme prévu! Merci ! – ProgSky