Stopwatch.Elapsed 屬性

定義

取得目前執行個體所測量的已耗用時間總和。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

屬性值

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 方法,然後使用屬性來檢查經過的時間 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.

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

您可以在 Elapsed ElapsedMilliseconds ElapsedTicks 實例正在執行或停止時,查詢屬性、和 StopwatchYou 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. Reset 可以使用方法來清除現有實例中的累積耗用時間 StopwatchUse the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

適用於

另請參閱