Stopwatch Constructor

Definición

Inicializa una nueva instancia de la clase Stopwatch.

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

Ejemplos

En el ejemplo siguiente se inicializa una Stopwatch instancia mediante un constructor de clase 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

Comentarios

La instancia devuelta Stopwatch se detiene y la propiedad de tiempo transcurrido de la instancia es cero.

Use el Start método para empezar a medir el tiempo transcurrido con la nueva Stopwatch instancia. Use el StartNew método para inicializar una nueva Stopwatch instancia e iniciarla inmediatamente.

Se aplica a

Consulte también