Timer::Interval Property

 

Gets or sets the time, in milliseconds, before the Tick event is raised relative to the last occurrence of the Tick event.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
property int Interval {
	int get();
	void set(int value);
}

Property Value

Type: System::Int32

An Int32 specifying the number of milliseconds before the Tick event is raised relative to the last occurrence of the Tick event. The value cannot be less than one.

To get the number of seconds in the interval, divide this number by 1,000.

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 ref class Class1
{
private:
   static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
   static int alarmCounter = 1;
   static bool exitFlag = false;

   // This is the method to run when the timer is raised.
   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?", String::Format( "Count is: {0}", 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 void Main()
   {

      /* Adds the event and the event handler for the method that will 
                process the timer event to the timer. */
      myTimer->Tick += gcnew 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();
      }
   }

};

int main()
{
   Class1::Main();
}

.NET Framework
Available since 1.1
Return to top
Show: