System.Timers

Timers are a great addition to the library. Using Events, you can add a timer to a Console application. Listing B.63 shows how to implement a timer (timer.cs).

Listing B.63. Timers Example
public class TimerMain
{
    private static long lastTickCount = 0;
    public static void OnElapsedEvent(object source, ElapsedEventArgs e)
    {
        if(lastTickCount == 0)
        {
            lastTickCount = Environment.TickCount;
            Console.WriteLine("Hello!!");
        }
        else
        {
            long currentTickCount = Environment.TickCount;
            Console.WriteLine("Hello!! {0} ", currentTickCount - lastTickCount);
            lastTickCount = currentTickCount;
        }
    }
    static void Main(string [] args)
    {
        Timer timer = new Timer();
        timer.Elapsed+=new ElapsedEventHandler(OnElapsedEvent);
        // Set the Interval to 1 second.
        timer.Interval = 1000;
        timer.Enabled = true;

        Console.WriteLine("Press <enter> to quit the sample.");
        Console.ReadLine();
    }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.240.178