C # Stopwatch detailed

Source: Internet
Author: User

C # Stopwatch Class

namespaces: System.Diagnostics;

Namespacesystem.diagnostics{Provides a set of methods and properties that can be used to accurately measure elapsed time.PublicClassStopwatch {Gets the timer frequency expressed as the number of ticks per second. This field is read-only.PublicStaticReadOnlyLong Frequency;Indicates whether the timer is based on high-resolution performance counters. This field is read-only.PublicStaticReadOnlyBOOL Ishighresolution;Initializes a new instance of the System.Diagnostics.Stopwatch class.PublicStopwatch ();Gets the total elapsed time measured by the current instance.return Result:A read-only System.TimeSpan that represents the total elapsed time measured by the current instance.Public TimeSpan Elapsed {Get }Gets the total elapsed time, in milliseconds, measured by the current instance.return Result:A read-only long integer that represents the total number of milliseconds measured by the current instance.PublicLong Elapsedmilliseconds {Get }Gets the total elapsed time measured by the current instance, expressed as a timer tick.return Result:A read-only long integer that represents the total number of timer ticks measured by the current instance.PublicLong Elapsedticks {Get }Gets a value that indicates whether the System.Diagnostics.Stopwatch timer is running.return Result:True if the System.Diagnostics.Stopwatch instance is currently running and is measured for the elapsed time of a time interval, otherwise false.PublicBOOL IsRunning {Get }Gets the current minimum number of time units in the timer mechanism.return Result:A long integer that represents the tick counter value in the underlying timer mechanism.PublicStaticLongGettimestamp ();Initializes the new System.Diagnostics.Stopwatch instance, sets the run time property to zero, and then starts measuring elapsed time.return Result:The System.Diagnostics.Stopwatch is just beginning to measure the running time.PublicStatic StopwatchStartNew ();//stop interval measurement and resets the run time to zero. public void reset (//stop interval measurement, resets the run time to zero, and then starts measuring elapsed time. public void restart (//start or continue to measure the elapsed time of an interval. public void start (//stop measuring the elapsed time of an interval. public void stop (        

The stopwatch namespace is a using System.Diagnostics;

From the above see restart () equals reset () plus start ()

The use process is as follows

new Stopwatch();//开始计时sw.Start();//重新设置为零sw.Reset();//重新设置并开始计时sw.Restart();//结束计时sw.Stop();//获取运行时间间隔TimeSpan ts = sw.Elapsed;//获取运行时间[毫秒]long times = sw.ElapsedMilliseconds;//获取运行的总时间long times2 = sw.ElapsedTicks;//判断计时是否正在进行[true为计时]bool isrun = sw.IsRunning;//获取计时频率long frequency = Stopwatch.Frequency;

Stopwatch instances can measure the elapsed time of a single time interval, or they can measure the total elapsed time of multiple time intervals. In a typical Stopwatch scenario, call the Start method, then call the Stop method, and finally use the Elapsed property to check the run time.

The Stopwatch instance is either running or stopped, and isrunning can be used to determine the current state of the Stopwatch. Start the measurement run time using start, or stop to measure the elapsed time using stop. Query run time values through properties Elapsed, Elapsedmilliseconds, or elapsedticks. You can query the run time properties when the instance is running or stopped. The run time property is steadily incremented during the Stopwatch run, and remains the same when the instance is stopped.

By default, the run time value of the Stopwatch instance is equal to the sum of all measured intervals. The cumulative run time count is started each time the start is called, and the current interval measurement is ended each time the Stop is called, and the accumulated run time value is frozen. Use the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

Article reproduced from: 52799830

(GO) C # stopwatch detailed

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.