Stopwatch.Elapsed Stopwatch.Elapsed Stopwatch.Elapsed Stopwatch.Elapsed Property

定義

取得目前執行個體所測量的已耗用時間總和。Gets the total elapsed time measured by the current instance.

public:
 property TimeSpan Elapsed { TimeSpan get(); };
public TimeSpan Elapsed { get; }
member this.Elapsed : TimeSpan
Public ReadOnly Property Elapsed As TimeSpan

屬性值

表示目前執行個體所測量之已耗用時間總和的唯讀 TimeSpanA read-only TimeSpan representing the total elapsed time measured by the current instance.

範例

下列範例示範如何使用Elapsed屬性來判斷應用程式的執行時間。The following example demonstrates how to use the Elapsed property to determine the execution time for 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.

使用屬性, 即可使用TimeSpan方法和屬性來抓取已耗用的時間值。 ElapsedUse the Elapsed property to retrieve the elapsed time value using TimeSpan methods and properties. 例如, 您可以將傳回TimeSpan的實例格式化為文字表示, 或將它傳遞給TimeSpan需要參數的另一個類別。For example, you can format the returned TimeSpan instance into a text representation, or pass it to another class that requires a TimeSpan parameter.

Stopwatch實例正在執行或已ElapsedMilliseconds停止時ElapsedTicks , 您可以查詢屬性Elapsed、和。You can query the properties Elapsed, ElapsedMilliseconds, and ElapsedTicks while the Stopwatch instance is running or stopped. Stopwatch已耗用時間屬性會在執行時穩定地增加; 當實例停止時, 它們會保持不變。The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

根據預設, Stopwatch實例的經過時間值會等於所有測量時間間隔的總計。By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. 每次呼叫Start都會在累計耗用時間開始計數; 每次Stop呼叫都會結束目前的間隔測量, 並凍結累計已耗用時間值。Each call to Start begins counting at the cumulative elapsed time; each call to Stop ends the current interval measurement and freezes the cumulative elapsed time value. 使用方法來清除現有Stopwatch實例中的累計已耗用時間。 ResetUse the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

適用於

另請參閱