Stopwatch.Stop 方法

定义

停止测量某个时间间隔的运行时间。Stops measuring elapsed time for an interval.

public:
 void Stop();
public void Stop ();
member this.Stop : unit -> unit
Public Sub Stop ()

示例

下面的示例演示如何使用Stop方法来停止测量应用程序的执行时间的计时器。The following example demonstrates how to use the Stop method to stop a timer that measures the execution time of an application.

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);
    }
}
Imports System.Diagnostics
Imports System.Threading


Class Program

    Shared Sub Main(ByVal args() As String)
        Dim stopWatch As New Stopwatch()
        stopWatch.Start()
        Thread.Sleep(10000)
        stopWatch.Stop()
        ' Get the elapsed time as a TimeSpan value.
        Dim ts As TimeSpan = stopWatch.Elapsed

        ' Format and display the TimeSpan value.
        Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
        Console.WriteLine( "RunTime " + elapsedTime)

    End Sub
End Class

注解

在典型Stopwatch的方案中, Start调用方法Stop , 最后调用方法, 然后使用Elapsed属性检查运行时间。In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

Stop方法结束当前时间间隔测量。The Stop method ends the current time interval measurement. 停止未运行的不会更改计时器状态, 也不会重置运行时间属性。 StopwatchStopping a Stopwatch that is not running does not change the timer state or reset the elapsed time properties.

如果实例测量多个间隔, 则该Stop方法等效于暂停经过的时间度量。 StopwatchWhen a Stopwatch instance measures more than one interval, the Stop method is equivalent to pausing the elapsed time measurement. 用于Start从当前运行时间值开始测量时间的后续调用。A subsequent call to Start resumes measuring time from the current elapsed time value. 使用方法可清除Stopwatch实例中的累计运行时间。 ResetUse the Reset method to clear the cumulative elapsed time in a Stopwatch instance.

适用于

另请参阅