Timer.Enabled Property
Assembly: System.Windows.Forms (in system.windows.forms.dll)
The following example implements a simple interval timer, which sets off an alarm every five seconds. When the alarm occurs, a MessageBox displays a count of the number of times the alarm has started and prompts the user whether the timer should continue to run.
public class Class1 { static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); static int alarmCounter = 1; static bool exitFlag = false; // This is the method to run when the timer is raised. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.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; myTimer.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. */ myTimer.Tick += new EventHandler(TimerEventProcessor); // Sets the timer interval to 5 seconds. myTimer.Interval = 5000; myTimer.Start(); // Runs the timer, and raises the event. while(exitFlag == false) { // Processes all the events in the queue. Application.DoEvents(); } return 0; } }
public class Class1
{
private static System.Windows.Forms.Timer myTimer =
new System.Windows.Forms.Timer();
private static int alarmCounter = 1;
private static boolean exitFlag = false;
// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
{
myTimer.Stop();
// Displays a message box asking whether to continue running the timer.
if (MessageBox.Show("Continue running?", "Count is: "
+ alarmCounter, MessageBoxButtons.YesNo).Equals(DialogResult.Yes)) {
// Restarts the timer and increments the counter.
alarmCounter += 1;
myTimer.set_Enabled(true);
}
else {
// Stops the timer.
exitFlag = true;
}
} //TimerEventProcessor
public static void main(String[] args)
{
/* Adds the event and the event handler for the method that will
process the timer event to the timer.
*/
myTimer.add_Tick(new EventHandler(TimerEventProcessor));
// Sets the timer interval to 5 seconds.
myTimer.set_Interval(5000);
myTimer.Start();
// Runs the timer, and raises the event.
while (exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return;
} //main
} //Class1
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Notes
Assigning true to Enabled when Enabled == false has the side effect of reseting the countdown.
- 7/30/2006
- Peter Ritchie