Stopwatch.Start 方法

定義

啟動或繼續測量間隔的已耗用時間。Starts, or resumes, measuring elapsed time for an interval.

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

範例

下列範例示範如何使用 Start 方法來啟動計時器來測量應用程式的執行時間。The following example demonstrates how to use the Start method to start a timer that measures the execution time of 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.

一旦啟動, Stopwatch 計時器就會測量目前的間隔(以經過時間的計時器刻度為單位),直到停止或重設實例為止。Once started, a Stopwatch timer measures the current interval, in elapsed timer ticks, until the instance is stopped or reset. 啟動 Stopwatch 已在執行中的,並不會變更計時器狀態,也不會重設經過的時間屬性。Starting a Stopwatch that is already running does not change the timer state or reset the elapsed time properties.

Stopwatch 實例測量一個以上的間隔時,此 Start 方法會繼續測量目前經過時間值的時間。When a Stopwatch instance measures more than one interval, the Start method resumes measuring time from the current elapsed time value. Stopwatch實例會計算並保留跨多個時間間隔的累計經過時間,直到實例重設為止。A Stopwatch instance calculates and retains the cumulative elapsed time across multiple time intervals, until the instance is reset. Reset 呼叫之前,請先使用方法 Start 來清除實例中的累積耗用時間 StopwatchUse the Reset method before calling Start to clear the cumulative elapsed time in a Stopwatch instance. 使用的 Restart 方法搭配 Reset Start Stopwatch 單一命令。Use the Restart method to Reset and Start the Stopwatch with a single command.

適用於

另請參閱