C#Timer–Simple use of timer class

Hits: 0

C#Timer–Simple use of timer class

System.Timers.Timer

A timer is to execute a fixed task after a fixed period of time.

The timer is easy to use

set initial properties

How long to execute the initialization timer interval

System.Timers.Timer timer = new System.Timers.Timer(double interval);
interval is the number of milliseconds. For example, the last millisecond is set to 1000, which is one second.
It can also be created with a no-argument constructor and set with object.interval.

Set the event to execute – Elapsed

timer.Elapsed += requires method name.
After the milliseconds set above, the event is executed. For example, if I want to execute the method MyMethod within the specified time, use the parameter Elapsed to specify. timer.Elapsed += MyMethod.

This method has one and only two parametersMyMethod(Object source, ElapsedEventArgs e), and the return value isvoid。The ElapsedEventArgs is the object that provides data to the Elapsed event.
Of course,You can also call other methods in MyMethod.

Set whether to repeat execution – AutoReset

The value type of this parameter isbool,The default isfalse。
If false, the timer will only run onExecute once after the specified time for the first timeThe Elapsed method will not be executed after the specified time interval.

if true thenExecute this method every time interval elapses。

Set whether to trigger the Elapsed event –Enabled

The value type of this parameter isbool,The default isfalse。
iffalse, even after the specified eventElapsed event is not fired.
iftrue, after the specified time, executeElapsed event。

Simple example – borrowing from the official example

using System;
using System.Timers;

public  class  TimerTest 
{ 
    //Define timer 
    private  static System.Timers.Timer aTimer;

    public  static  void  Main ()
     {
         //Initialize timer properties
        SetTimer ();

        Console.WriteLine( "\nKeyboard input to exit...\n" );
        Console.WriteLine( "Timer starts at {0:HH:mm:ss.fff}" , DateTime.Now);
        Console.ReadLine(); //Keep the program running 
        aTimer.Stop(); //Stop the timer 
        Console.WriteLine( "\nStop the timer...\n" );
        aTimer.Dispose(); //Release resources 
        Console.WriteLine( "Exit..." );
        Console.ReadLine();
    }

    private static void SetTimer()
    {
        Console.WriteLine( "Initialize the current timer" );
         // Create a timer every two seconds 
        aTimer = new System.Timers.Timer( 2000 );
         // Set the event
        aTimer.Elapsed += MyMethod;
        aTimer.AutoReset = true;
        aTimer.Enabled = true;
        Console.WriteLine( "Setting up" );
    }


    public  static  void  A ()
     {
         //Write the real operation here 
        Console.WriteLine( "I scheduled operation A" );
    }


    private  static  void  MyMethod (Object source , ElapsedEventArgs e)
     {
         //The operation I really need to perform 
        Console.WriteLine( "Current time {0:HH:mm:ss.fff}" , DateTime.Now);
        Console.WriteLine( "Data carried by ElapsedEventArgs {0:HH:mm:ss.fff}" , e.SignalTime);
        A();
    }
}

result

[Official documentation is here]

Leave a Reply

Your email address will not be published.