Stopwatch 建構函式

定義

初始化 Stopwatch 類別的新執行個體。Initializes a new instance of the Stopwatch class.

public:
 Stopwatch();
public Stopwatch ();
Public Sub New ()

範例

下列範例會 Stopwatch 使用簡單類別的函式來初始化實例。The following example initializes a Stopwatch instance by using a simple class constructor.

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 實例已停止,且實例的經過時間屬性為零。The returned Stopwatch instance is stopped, and the elapsed time property of the instance is zero.

Start 可以使用方法,開始使用新的實例來測量經過的時間 StopwatchUse the Start method to begin measuring elapsed time with the new Stopwatch instance. 使用 StartNew 方法來初始化新的 Stopwatch 實例,並立即啟動它。Use the StartNew method to initialize a new Stopwatch instance and immediately start it.

適用於

另請參閱