How do you add a timer to a C# console application

How do you add a timer to a C# console application:
However, the style of using a for loop to do some functionality forever takes a lot of device resources and instead we can use the Garbage Collector to do some thing like that.

For the use of timer in C# use fallowing :

using System;
using System.Threading;
We can see this modification code  in the code in C# 
using System;
using System.Threading;

public static class Program 
{

   public static void Main()
 {

      // Create a Timer object that knows to call our TimerCallback
      // method once every 2000 milliseconds.
      Timer t = new Timer(TimerCallback, null, 0, 2000);
      // Wait for the user to hit <Enter>
      Console.ReadLine();

   }

   private static void TimerCallback(Object o) 
    {

      // Display the date/time when this method got called.
      Console.WriteLine("In TimerCallback: " + DateTime.Now);
      // Force a garbage collection to occur for this demo.
      GC.Collect();

    }

}






Asp.net related other Post :






Comments

Popular posts from this blog