Return to Snippet

Revision: 19821
at October 31, 2009 22:38 by rtipton


Initial Code
using System;
using System.Windows.Forms;

namespace WindowsFormsTimer
{
    public class Class1
    {
        static System.Windows.Forms.Timer theTimer = new System.Windows.Forms.Timer();
        static int alarmCounter = 1;
        static bool exitFlag = false;

        // Timer raised method
        private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
        {
            theTimer.Stop();

            // Displays a message box asking whether to continue running the timer
            if (MessageBox.Show("Continue running?", "Count is " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // Restarts the timer and increments the counter
                alarmCounter += 1;
                theTimer.Enabled = true;
            }
            else
            {
                // Stops the timer
                exitFlag = true;
            }
        }

        public static int Main()
        {
            //Adds the event and the event handler for the method that will 
            //process the timer event to the timer
            theTimer.Tick += new EventHandler(TimerEventProcessor);

            // Sets the timer interval to 5 seconds
            theTimer.Interval = 2000;
            theTimer.Start();

            // Runs the timer, and raises the event
            while (exitFlag == false)
            {
                // Processes all the events in the queue
                Application.DoEvents();
            }
            return 0;
        }
    }
}

Initial URL


Initial Description


Initial Title
System.Windows.Forms.Timer Example

Initial Tags
forms, windows

Initial Language
C#