Stopwatch コンストラクター

定義

Stopwatch クラスの新しいインスタンスを初期化します。

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

次の例では、 Stopwatch 単純なクラスコンストラクターを使用して、インスタンスを初期化します。

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 インスタンスの経過時間プロパティが0になっています。

メソッドを使用して Start 、新しいインスタンスで経過時間の計測を開始し Stopwatch ます。 メソッドを使用して、 StartNew 新しい Stopwatch インスタンスを初期化し、すぐに開始します。

適用対象

こちらもご覧ください