Stopwatch.Start 方法

定义

开始或继续测量某个时间间隔的运行时间。

public:
 void Start();
public void Start ();
member this.Start : unit -> unit
Public Sub Start ()

示例

下面的示例演示如何使用 Start 方法启动一个计时器,用于测量应用程序的执行时间。

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

启动后, Stopwatch 计时器将测量当前间隔(以已用计时器计时周期为单位),直到实例停止或重置。 启动 Stopwatch 已运行的 不会更改计时器状态或重置已用时间属性。

Stopwatch当实例测量多个间隔时Start,该方法将从当前已用时间值恢复测量时间。 实例 Stopwatch 计算并保留多个时间间隔的累积运行时间,直到实例重置为止。 在调用 Start 之前使用 Reset 方法清除 实例中的Stopwatch累积运行时间。 使用 Restart 方法 Reset ,将 和 StartStopwatch 与单个命令配合使用。

适用于

另请参阅