Stopwatch.Stop 方法

定义

停止测量某个时间间隔的运行时间。

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

示例

以下示例演示如何使用 Stop 方法停止测量应用程序执行时间的计时器。

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

方法 Stop 结束当前时间间隔度量。 停止 Stopwatch 未运行的 不会更改计时器状态或重置已用时间属性。

当实例 Stopwatch 测量多个间隔时 Stop ,方法等效于暂停已用时间度量。 对 的后续调用 Start 将从当前已用时间值恢复测量时间。 Reset使用 方法清除实例中的Stopwatch累积已用时间。

适用于

另请参阅