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累積經過的時間。

適用於

另請參閱