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 方法,然後使用屬性來檢查經過的時間 ElapsedIn 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. 停止未 Stopwatch 執行的,並不會變更計時器狀態,也不會重設經過的時間屬性。Stopping a Stopwatch that is not running does not change the timer state or reset the elapsed time properties.

Stopwatch 實例測量一個以上的間隔時, Stop 方法就相當於暫停經過的時間量測。When 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. Reset 可以使用方法來清除實例中的累積耗用時間 StopwatchUse the Reset method to clear the cumulative elapsed time in a Stopwatch instance.

適用於

另請參閱