Simple Implementation Example of Timer in C

  • 2021-12-12 09:30:56
  • OfStack

This paper describes the simple implementation method of timer in C #. Share it for your reference, as follows:


startTime = DateTime.Now;
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 1);
dt.Tick += new EventHandler(dt_Tick);// Call function 
dt.Start();
void dt_Tick(object sender, EventArgs e)
{
   timeSpan = DateTime.Now.Subtract(startTime);
   // Use TimeSpan To format the time 
   timer1.Content = string.Format("{0:00}:{1:00}:{2:00}", (int)timeSpan.TotalHours,timeSpan.Minutes, timeSpan.Seconds);
   // Use DateTime To format the time 
   timer2.Content = String.Format("{0:HH:mm:ss}", new DateTime(timeSpan.Ticks));
}

For more readers interested in C # related content, please check the topics on this site: "C # Date and Time Operation Skills Summary", "C # String Operation Skills Summary", "C # Array Operation Skills Summary", "XML File Operation Skills Summary in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: