C# - Multiple timers in Windows Service

10,326

Solution 1

I don't see the reason why not. System.Timers.Timer raise an event in the separate thread from ThreadPool so they are independent from each other. The only thing you need to make sure that code in your callback will not be executing longer than the Interval. If you think it may happen, you need to stop timer in the event handler and recreate it in the end.

private System.Timers.Timer timer1;

protected override void OnStart(string[] args)
{
    this.timer1 = new System.Timers.Timer(30000) { AutoReset = false };
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
    this.timer1.Start(); 
}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    try 
    {
        // Do Work
    }
    catch (Exception ex) 
    {
        // Logging
    }
    finally 
    {
        timer1.Start()
    }
}

If the SynchronizingObject property is null, the Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In this situation, the event handler should be reentrant.

https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

Solution 2

Yes, you can use any number of timers - they are absolutely independent

Share:
10,326
Stephane
Author by

Stephane

Updated on June 05, 2022

Comments

  • Stephane
    Stephane almost 2 years

    I just start develop a windows service. But I'm not sure if I can use multiple timers in Onstart() method?

    private System.Timers.Timer timer1;
    private System.Timers.Timer timer2;
    
    
            protected override void OnStart(string[] args)
            {
                this.timer1 = new System.Timers.Timer(30000);  // every 30 seconds
                this.timer1.AutoReset = true;
                this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(fonction1);
                this.timer1.Start();
              
              
              this.timer2 = new System.Timers.Timer(10 * 60 * 1000); // every 10 minutes
                this.timer2.AutoReset = true;
                this.timer2.Elapsed += new System.Timers.ElapsedEventHandler(fonction2);
                this.timer2.Start();
              
              
            }
    
    
           private void fonction1(object sender, System.Timers.ElapsedEventArgs e)
            {
                bla bla
            }
    
           private void fonction2(object sender, System.Timers.ElapsedEventArgs e)
            {
                bla bla
            }

    fonction1 and fonction2 run at the same time but they don't have the same interval of running. Their subsequent start times don't need to be in sync.In this case,

    1. Can I use 2 timers in OnStart() method?
    2. And if so, does fonction1 which typically run every 30 seconds have to wait the end of fonction2 to the next start?

    I need to run these 2 methods with different intervals in a same Windows Service.