This article summarizes the usage of various timers in C#. Share it for your reference, as follows:
1. Use the Stopwatch class ()
The Stopwatch instance can measure the run time of one time interval or the total run time of multiple time intervals. In a typical Stopwatch scheme, the Start method is called first, then the Stop method is called, and finally the run time is checked using the Elapsed property.
The Stopwatch instance is either running or has stopped; using IsRunning to determine the current status of the Stopwatch. Use Start to start measuring runtime; Stop to stop measuring runtime. Query the runtime value by the attributes Elapsed, ElapsedMilliseconds, or ElapsedTicks. When the instance is running or has stopped, the runtime attribute can be queried. The runtime property increases steadily during Stopwatch runs; remains unchanged when the instance stops.
By default, the runtime value of the Stopwatch instance is equivalent to the sum of all measured time intervals. The cumulative run time count starts each time Start is called; the current interval measurement is ended every time Stop is called, and the cumulative run time value is frozen. Use the Reset method to clear the cumulative run time in an existing Stopwatch instance.
Stopwatch counts the timer's scale in the basic timer mechanism, thereby measuring the run time. If the installed hardware and operating system support a high-resolution performance counter, the Stopwatch class will use this counter to measure the runtime; otherwise, the Stopwatch class will use the system counter to measure the runtime. Use the Frequency and IsHighResolution fields to determine the accuracy and resolution of implementing Stopwatch timing.
Example
stopwatch = new (); (); //Task 1...(); _result.Text += "<p>Task 1 Time:" + + "。</p>"; (); //If there is no Reset, the time spent on task 1 will be accumulated into task 2(); //Task 2...(); _result.Text += "<p>Task 2 Time:" + + "。</p>";
2. Standard timer based on Windows ()
Windows timer is designed for a single-threaded environment. This timer is the easiest to use. Just drag the Timer control in the toolbox to the form, and then set the events and intervals and other properties.
3. Server-based timer()
It does not depend on the form, wakes up threads from the thread pool, and is an updated version optimized by traditional timers for running on the server environment.
4. Thread timer()
Thread timers also do not rely on forms, are simple, lightweight timers that use callback methods instead of events and are powered by thread pool threads.
5、
The TickCount property is used to get the millisecond count from the computer's system timer.
usage:
int startTime=; //......Task......int endTime=; int runTime=endTime-startTime;//(Note that the unit is milliseconds!)
6. Use TimeSpan class()
The TimeSpan object represents a time interval or duration, measured by positive and negative days, hours, minutes, seconds, and fractional parts of seconds. The maximum time unit used to measure duration is days. The number of days for larger units of time (such as months and years) is different, so to maintain consistency, time intervals are measured in days.
The value of the TimeSpan object is the number of ticks equal to the time interval represented. A scale equals 100 nanoseconds, and the value of the TimeSpan object ranges between MinValue and MaxValue.
The TimeSpan value can be expressed as [-]:mm:, where the minus sign is optional, which indicates negative time interval, d component represents days, hh represents hours (24-hour system), mm represents minutes, ss represents seconds, and ff is the fractional part of seconds. That is, the time interval includes the total number of positive and negative days, the number of days and the remaining time of less than one day, or only the time of less than one day. For example, the text of a TimeSpan object initialized to a 1.0e+13 scale represents "11.13:46:40", i.e. 11 days, 13 hours, 46 minutes, and 40 seconds.
usage:
startTime,endTime; time; startTime=; //......Task......endTime=; time=endTime-startTime; int runTime=;//(in milliseconds)
For more information about C# related content, please check out the topic of this site:Summary of C# date and time operation skills》、《Summary of C# string operation skills》、《Summary of C# array operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.