Stopwatch Constructeur

Définition

Initialise une nouvelle instance de la classe Stopwatch.

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

Exemples

L’exemple suivant initialise une Stopwatch instance à l’aide d’un constructeur de classe simple.

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

Remarques

L’instance retournée Stopwatch est arrêtée et la propriété de temps écoulé de l’instance est égale à zéro.

Utilisez la Start méthode pour commencer à mesurer le temps écoulé avec la nouvelle Stopwatch instance. Utilisez la StartNew méthode pour initialiser une nouvelle Stopwatch instance et la démarrer immédiatement.

S’applique à

Voir aussi