A summary of the usage of various timers in C

  • 2021-12-19 06:30:27
  • OfStack

This article summarizes various timer uses in C # with examples. Share it for your reference, as follows:

1. Use the Stopwatch class (System. Diagnostics. Stopwatch)

The Stopwatch instance can measure the elapsed time for 1 time interval or the total elapsed time for multiple time intervals. In a typical Stopwatch scenario, the Start method is called first, then the Stop method is called, and finally the Elapsed property is used to check the runtime.

The Stopwatch instance is either running or stopped; Use IsRunning to determine the current state of Stopwatch. Using Start, you can start measuring the running time; Using Stop, you can stop measuring the running time. The runtime value is queried through the property Elapsed, ElapsedMilliseconds, or ElapsedTicks. When the instance is running or stopped, you can query the runtime property. The runtime attribute is steadily incremented during Stopwatch operation; It remains unchanged when the instance stops.

By default, the elapsed time value of an Stopwatch instance is equal to the sum of all measured time intervals. Start accumulating runtime count every time Start is called; Ends the current interval measurement and freezes the cumulative elapsed time value each time Stop is called. Use the Reset method to clear the cumulative runtime in an existing Stopwatch instance.

Stopwatch measures run time by counting the timer's scale in the underlying timer mechanism. If the installed hardware and operating system supports a counter with high-resolution performance, the Stopwatch class will use this counter to measure the runtime; Otherwise, the Stopwatch class will use system counters to measure the runtime. The Frequency and IsHighResolution fields can be used to determine the accuracy and resolution of the Stopwatch timing.

Example


System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
// Mission  1...
stopwatch.Stop();
_result.Text += "<p> Mission  1  Time: " + stopwatch.ElapsedTicks + " . </p>";
stopwatch.Reset(); // If not  Reset Will put the task  1  Cumulative time spent on entering tasks  2
stopwatch.Start();
// Mission  2...
stopwatch.Stop();
_result.Text += "<p> Mission  2  Time: " + stopwatch.ElapsedTicks + " . </p>";

2. Standard timer based on Windows (System. Windows. Forms. Timer)

The Windows timer is designed for a single-threaded environment. This timer is the simplest one to use. Just drag the Timer control in the toolbox onto the form, and then set the properties such as event and interval under 1.

3. Server-based timer (System. Timers. Timer)

System. Timers. Timer does not rely on forms, wakes threads from a thread pool, and is an updated version of the traditional timer optimized to run on a server environment.

4. Thread Timer (System. Threading. Timer)

Thread Timer is also a simple lightweight timer that does not depend on forms and uses callback methods instead of events and is supported by thread pool threads.

5. System. Environment. TickCount

The TickCount property is used to get the millisecond count of the system timer from the computer.

Usage:


int startTime=System.Environment.TickCount;
//...... Mission ......
int endTime=System.Environment.TickCount;
int runTime=endTime-startTime;//( Note that the unit is milliseconds! )

6. Use the TimeSpan class (System. TimeSpan)

The TimeSpan object represents an interval or duration, measured in plus or minus days, hours, minutes, seconds, and fractional parts of seconds. The maximum time unit used to measure duration is days. Larger units of time (such as months and years) have different days, so to maintain uniformity, time intervals are measured in days.

The value of the TimeSpan object is the number of ticks equal to the time interval represented. One scale equals 100 nanoseconds, and the value of the TimeSpan object ranges between MinValue and MaxValue.

The TimeSpan value can be expressed as [-] d. hh: mm: ss. ff, where the minus sign is optional and indicates a negative time interval, the d component represents days, hh represents hours (24-hour system), mm represents minutes, ss represents seconds, and ff is a fractional part of seconds. That is, the time interval includes the whole plus or minus days, the days, and the remaining duration of less than 1 day, or only the duration of less than 1 day. For example, the text of an TimeSpan object initialized to a 1.0 e+13 scale represents "11.13: 46: 40", that is, 11 days, 13 hours, 46 minutes, and 40 seconds.

Usage:


System.DateTime startTime,endTime;
System.TimeSpan time;
startTime=System.DateTime.Now;
//...... Mission ......
endTime=System.DateTime.Now;
time=endTime-startTime;
int runTime=time.Milliseconds;//( In milliseconds )

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: