Share via


Stopwatch Costruttore

Definizione

Inizializza una nuova istanza della classe Stopwatch.

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

Esempio

Nell'esempio seguente viene inizializzata un'istanza usando un Stopwatch costruttore di classi semplice.

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

Commenti

L'istanza restituita viene arrestata Stopwatch e la proprietà time trascorsa dell'istanza è zero.

Usare il Start metodo per iniziare a misurare il tempo trascorso con la nuova Stopwatch istanza. Usare il StartNew metodo per inizializzare una nuova Stopwatch istanza e avviarlo immediatamente.

Si applica a

Vedi anche