Stopwatch.Stop 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
暫停測量經過時間一段時間。
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 經過的時間。