Using Timers in C#

Example 1: In Windows Forms applications Create a new Windows Forms Application: csharptimers1 For WinForms applications we make use of the System.Windows.Forms.Timer [code language="csharp"] private static readonly Timer Timer = new Timer(); [/code] Some common Timer tasks are to: 1. Subscribe to the event that is called when the interval has elapsed: [code language="csharp"] Timer.Tick += TimerEventProcessor; [/code] 2. Set the Timer interval: [code language="csharp"] Timer.Interval = 5000; [/code] 3. Start the timer (these are equivalent): [code language="csharp"] Timer.Start(); Timer.Enabled = true; [/code] 4. Stop the timer (these are equivalent): [code language="csharp"] Timer.Stop(); Timer.Enabled = false; [/code] Example Usage: [code language="csharp"] using System; using System.Windows.Forms; namespace TimerAppWinForms { public partial class Form1 : Form { private const string Caption = "Interval elapsed. Continue running?"; private static readonly Timer Timer = new Timer(); public Form1() { InitializeComponent(); Timer.Tick += TimerEventProcessor; Timer.Interval = 5000; Timer.Start(); } // This is the method to run when the timer is raised. private static void TimerEventProcessor(object myObject, EventArgs myEventArgs) { Timer.Stop(); var result = MessageBox.Show(Caption, "", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Timer.Start(); } } } } [/code] When run we are initially presented with the default form: csharptimers2 And after the 5 second (5000 millisecond) interval has elapsed, the function that handles this event is run, presenting the user with a dialog to either continue with the timed event or exit: csharptimers3 Example 2: In Console Applications Create a new Console Application: csharptimers4 When using timers in Console applications we must instead use System.Timers.Timer: [code language="csharp"] var timer = new System.Timers.Timer(); [/code] As with the previous WinForms example, we subscribe the appropriate event (this time called ‘Elapsed’), set the time interval and kick off the timer: [code language="csharp"] timer.Elapsed += new ElapsedEventHandler(TimerEventProcessor); timer.Interval = 5000; timer.Start(); [/code] Full code sample looks like this: [code language="csharp"] using System; using System.Timers; namespace TimerAppConsole { internal class Program { private static void Main(string[] args) { var timer = new Timer(); timer.Elapsed += TimerEventProcessor; timer.Interval = 5000; timer.Start(); Console.WriteLine("Press \'q\' to quit the sample."); while (Console.Read() != 'q') { } } private static void TimerEventProcessor(object myObject, EventArgs myEventArgs) { Console.WriteLine("Time Elapsed..."); } } } [/code] Giving the following Console output: csharptimers5

Comments

Popular posts from this blog

Using the Supervisor Controller Pattern to access View controls in MVVM

Getting started with client-server applications in C++

How to send an e-mail via Google SMTP using C#