none
timer job sample RRS feed

Answers

  • // Here's one very simple example with console.

    using System; using System.Diagnostics; using System.Timers; namespace TimerSample { class Program { static bool exit = false; static bool started = false; static Timer timer; static Stopwatch watch; static void Main(string[] args) { // keeps track of run time watch = new Stopwatch(); // timer timer = new Timer(); // invoke Elapsed event handler between 2 seconds timer.Interval = TimeSpan.FromSeconds(2).TotalMilliseconds; // method to execute when timer elapses timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); Console.WriteLine("Press any key to exit..."); // wait for key press to exit while (!exit) { if (!started) { watch.Start(); started = true; timer.Start(); } if (Console.KeyAvailable) { ConsoleKeyInfo keyInfo = Console.ReadKey(); exit = true; } } timer.Stop(); timer.Elapsed -= new ElapsedEventHandler(timer_Elapsed); timer.Dispose(); } static void timer_Elapsed(object sender, ElapsedEventArgs e) { // work here is just to print elapsed milliseconds Console.WriteLine("Time elapsed {0} ms", watch.ElapsedMilliseconds); } } }



    Saturday, August 4, 2012 9:37 AM