Posted By

rtipton on 10/31/09


Tagged

forms windows timer


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


System.Windows.Forms.Timer Example


 / Published in: C#
 

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace WindowsFormsTimer
  5. {
  6. public class Class1
  7. {
  8. static System.Windows.Forms.Timer theTimer = new System.Windows.Forms.Timer();
  9. static int alarmCounter = 1;
  10. static bool exitFlag = false;
  11.  
  12. // Timer raised method
  13. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
  14. {
  15. theTimer.Stop();
  16.  
  17. // Displays a message box asking whether to continue running the timer
  18. if (MessageBox.Show("Continue running?", "Count is " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes)
  19. {
  20. // Restarts the timer and increments the counter
  21. alarmCounter += 1;
  22. theTimer.Enabled = true;
  23. }
  24. else
  25. {
  26. // Stops the timer
  27. exitFlag = true;
  28. }
  29. }
  30.  
  31. public static int Main()
  32. {
  33. //Adds the event and the event handler for the method that will
  34. //process the timer event to the timer
  35. theTimer.Tick += new EventHandler(TimerEventProcessor);
  36.  
  37. // Sets the timer interval to 5 seconds
  38. theTimer.Interval = 2000;
  39. theTimer.Start();
  40.  
  41. // Runs the timer, and raises the event
  42. while (exitFlag == false)
  43. {
  44. // Processes all the events in the queue
  45. Application.DoEvents();
  46. }
  47. return 0;
  48. }
  49. }
  50. }

Report this snippet  

You need to login to post a comment.