How to use timer in C#

1 Answer

0 votes
using System;
using System.Timers;
  
public class Program
{
    public static void Main(string[] args) {
        Timer timer = new Timer();
        
        timer.Elapsed += new ElapsedEventHandler(Event);
        timer.Interval = 3000;
        timer.Enabled = true;

        Console.WriteLine("Press \'q\' to quit");
        while(Console.Read() != 'q');

        timer.Stop();
        timer.Dispose();
  }

  private static void Event(object source, ElapsedEventArgs e) {
      Console.WriteLine("3 second count");
  }
}
  
  
  
  
/*
run:
    
Press 'q' to quit
3 second count
3 second count
3 second count
3 second count
    
*/

 



answered Aug 2, 2022 by avibootz
...