Timer.Interval Property
.NET Framework 2.0
Gets or sets the time, in milliseconds, between timer ticks.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Assembly: System.Windows.Forms (in system.windows.forms.dll)
The following code 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.
Minimum Interval
The minimum acceptable value for Interval is 10. If this value is less than 10 and greater than or equal to 0, 10 will be used instead. A value of less than 0 will result in a value of 10 being used on Windows NT/2000/XP and a value of 2,147,483,647 being used on Windows Server 2003/XP SP2.
- 10/28/2008
- Peter Ritchie