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,用于表示当前实例测量得出的总运行时间。A 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.

可以在Elapsed Stopwatch实例正在运行或ElapsedMilliseconds已停止ElapsedTicks时查询属性、和。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.

适用于

另请参阅