View the running time-Stopwatch class

Source: Internet
Author: User

A class in the System. Diagnostics namespace.

In fact, it is equivalent to a stopwatch. It can be stopped more than once. The Start method is to press the stopwatch to Start timing. The Stop method is to press the stopwatch again to pause timing. The IsRunning attribute is used to determine whether the stopwatch is running. The Reset method is to clear the stopwatch.

Its mechanism is as follows:
If the installed hardware or operating system provides a high-resolution counter, use that counter; otherwise, the system timer. Frequency and IsHighResolution are used to determine the accuracy and whether the resolution is high.

TIPS:
Which of the following cores does not affect the processing of multi-core computers?

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Diagnostics;
Using System. Threading;

Namespace _ 090914_ApplicationRunSpan1
{
Class Program
{
Static void Main (string [] args)
{
Stopwatch watcher = new Stopwatch ();
Watcher. Start ();
Thread. Sleep (2500 );
If (watcher. IsRunning)
Console. WriteLine ("The watcher is running .");
Watcher. Stop ();
Console. WriteLine (watcher. ElapsedMilliseconds. ToString () + "Milliseconds ");
TimeSpan ts = watcher. Elapsed;
Console. WriteLine (String. Format ("{0: 00 }",
Ts. Hours, ts. Minutes, ts. Seconds,
Ts. Milliseconds/10 ));
Console. Read ();
}
}
}

When watcher. IsRunning was not added to the program above, Sleep also experienced 2500 ms. However, it seems that only Sleep is 2499, which is also random.

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.