Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

timer
c#

How to create a timer in C#

Aaron Xie

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

widget

To create a timer in C#, you have to use the Timer class, which allows you to execute methods at a specified interval. Below is a simple example of a timer using C#.

using System;
using System.Threading;
public static class Program {
public static void Main() {
Timer t = new Timer(TimerCallback, null, 0, 1000);
Console.ReadLine();
}
private static void TimerCallback(Object o) {
Console.WriteLine("In TimerCallback: " + DateTime.Now);
}
}
  • Uses the System.Threading namespace, which allows you access various classes and interfaces that enable multi-threaded programming, like the Timer class (Line 2).

  • Creates a Timer object that will call the TimerCallback function every 1000 milliseconds. The null value is for the state parameter, which allows you to pass any information to be used in the callback method. The 0 is for the dueTime variable, which is the time of delay before the callback is called (Line 7).

  • Waits for the user to hit Enter, which will stop the timer (Line 8).

  • Creates the function TimerCallback, which will be called every 1000 milliseconds from the Timer object. Object o is passed in, since the TimerCallback function requires an object parameter, even if you don’t use it (Line 11).

  • Logs the time in the console every second, or 1000 milliseconds (Line 12).

RELATED TAGS

timer
c#

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring