Elapsed Property
Collapse the table of content
Expand the table of content

Stopwatch.Elapsed Property

Note: This property is new in the .NET Framework version 2.0.

Gets the total elapsed time measured by the current instance.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

public TimeSpan Elapsed { get; }
/** @property */
public TimeSpan get_Elapsed ()

public function get Elapsed () : TimeSpan

Property Value

A read-only TimeSpan representing the total elapsed time measured by the current instance.

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.

Use the Elapsed property to retrieve the elapsed time value using TimeSpan methods and properties. For example, you can format the returned TimeSpan instance into a text representation, or pass it to another class that requires a TimeSpan parameter.

You can query the properties Elapsed, ElapsedMilliseconds, and ElapsedTicks while the Stopwatch instance is running or stopped. The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. 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. Use the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

The following example illustrates retrieving the elapsed time of a Stopwatch instance and formatting the value into text representation. This code example is part of a larger example provided for the Stopwatch class.

// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpan value.
labelTime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
    ts.Hours, ts.Minutes, ts.Seconds, 
    ts.Milliseconds/10);

// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.get_Elapsed();
// Format and display the TimeSpan value.
labelTime.set_Text(((System.Int32)ts.get_Hours()).ToString("00") 
    + ":" + ((System.Int32)ts.get_Minutes()).ToString("00") 
    + ":" + ((System.Int32)ts.get_Seconds()).ToString("00") 
    + "." + ((System.Int32)(ts.get_Milliseconds() / 10)).
    ToString("00"));

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2016 Microsoft