Advertisement
jyoung12387

Timer Example

Mar 5th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2. using System.Timers;
  3.  
  4. namespace TimerApp
  5. {
  6.     class Program
  7.     {
  8.         private static Timer aTimer;
  9.         static void Main(string[] args)
  10.         {
  11.             // Create a timer and set a two second interval
  12.             aTimer = new Timer();
  13.             aTimer.Interval = 2000;
  14.  
  15.             // Hook up the Elapsed event for the timer.
  16.             aTimer.Elapsed += OnTimedEvent;
  17.  
  18.             //Have the timer fire repeated events (true is the default)
  19.             aTimer.AutoReset = true;
  20.  
  21.             //Start the timer
  22.             aTimer.Enabled = true;
  23.  
  24.             Console.WriteLine("Press the Enter key to exit the program...");
  25.             Console.ReadLine();
  26.         }
  27.         private static void OnTimedEvent(object source, ElapsedEventArgs e)
  28.         {
  29.             Console.WriteLine("Elapsed event was raised at: {0:HH:mm:ss}.", e.SignalTime);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement