Various timers in C #

Source: Internet
Author: User
1. Use the stopwatch class (system. Diagnostics. stopwatch)

The stopwatch instance can measure the running time at one interval or the total running time at multiple intervals. In a typical stopwatch solution, call the start method, call the stop method, and use the elapsed attribute to check the running time.

The stopwatch instance is either running or stopped. You can use isrunning to determine the current status of stopwatch. Use start
You can start measuring the running time. You can use stop to stop measuring the running time. Elapsed, elapsedmilliseconds, or
Elapsedticks queries the running time value. You can query the running time attribute when the instance is running or stopped. Run Time attribute in stopwatch
It increases steadily during running; it remains unchanged when the instance is stopped.

By default, stopwatch
The running time value of the instance is equivalent to the total time interval of all measurements. The total running time count starts when start is called each time.
End the current interval measurement, and freeze the cumulative running time value. You can use the reset method to clear the accumulated running time of an existing stopwatch instance.

In the Basic timer mechanism, stopwatch counts the timer scale to measure the running time. If the installed hardware and Operating System Support counters with high-resolution performance
The stopwatch class uses this counter to measure the running time; otherwise, the stopwatch class uses the system counter to measure the running time. Use Frequency and
The ishighresolution field can determine the precision and resolution of stopwatch timing.

Example
System. Diagnostics. Stopwatch stopwatch = new system. Diagnostics. Stopwatch ();

Stopwatch. Start ();
// Task 1...
Stopwatch. Stop ();
_ Result. Text + = "<p> Task 1 time:" + stopwatch. ElapsedTicks + ". </P> ";

Stopwatch. Reset (); // if there is no Reset, the total time of Task 1 is entered in Task 2.
Stopwatch. Start ();
// Task 2...
Stopwatch. Stop ();
_ Result. Text + = "<p> Task 2 time:" + stopwatch. ElapsedTicks + ". </P> ";

2. Windows-based standard Timer (System. Windows. Forms. Timer)

The Windows Timer is designed for a Single-threaded environment. This Timer is the simplest to use. You only need to drag the Timer control in the toolbox to the form, set the attributes such as the event and interval.

3. server-based Timer (System. Timers. Timer)

System. Timers. Timer does not rely on the form. It is used to wake up the thread from the thread pool, and is the updated version after the traditional Timer is optimized to run in the server environment.

4. Thread Timer (System. Threading. Timer)

The thread timer does not depend on the form. It is a simple and lightweight timer. It uses the callback method rather than the event and is supported by the thread pool thread.

5. System. Environment. TickCount

The TickCount attribute is used to obtain the millisecond count of the system timer from the computer.

Usage:

Int startTime = System. Environment. TickCount;
... Task ......
Int endTime = System. Environment. TickCount;
Int runTime = endTime-startTime; (Note that the unit is millisecond !)

6. Use the TimeSpan class (System. TimeSpan)

The TimeSpan object indicates the time interval or duration, which is measured by plus or minus days, hours, minutes, seconds, and decimals of seconds. The maximum unit of time used to measure the duration is day. A larger unit of time (such as Month and year) has different days. Therefore, to maintain consistency, the time interval is measured in days.

The value of a TimeSpan object is equal to the number of scales for the time interval. A scale is equal to 100 nanoseconds. The value of a TimeSpan object ranges from MinValue to MaxValue.

The TimeSpan value can be [-] d. hh: mm: ss. ff. The minus sign is optional. It indicates the negative time interval. The d Component indicates the day, hh indicates the hour (in 24-hour format), mm indicates the minute, and ss indicates the second, ff is the fractional part of the second. That is, the time interval includes the total plus or minus days, the number of days, and the remaining duration of less than one day, or only the duration of less than one day. For example, the text of a TimeSpan object initialized to 1.0e + 13 indicates "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;

... Task ......

EndTime = System. DateTime. Now;

Time = endTime-startTime;

Int runTime = time. Milliseconds; (in Milliseconds)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.