Condividi tramite


Stopwatch.Stop Metodo

Definizione

Interrompe la misurazione del tempo trascorso per un intervallo.

public:
 void Stop();
public void Stop();
member this.Stop : unit -> unit
Public Sub Stop ()

Esempio

Nell'esempio seguente viene illustrato come usare il Stop metodo per arrestare un timer che misura il tempo di esecuzione di un'applicazione.

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

In uno scenario tipico Stopwatch , chiamare il metodo , quindi chiamare il StartStop metodo e quindi controllare il tempo trascorso usando la Elapsed proprietà .

Il Stop metodo termina la misurazione dell'intervallo di tempo corrente. L'arresto di un Stopwatch oggetto che non è in esecuzione non modifica lo stato del timer o reimposta le proprietà temporali trascorse.

Quando un'istanza Stopwatch misura più di un intervallo, il Stop metodo equivale a sospendere la misurazione del tempo trascorso. Una chiamata successiva per Start riprendere la misurazione del tempo dal valore di tempo trascorso corrente. Utilizzare il Reset metodo per cancellare il tempo trascorso cumulativo in un'istanza Stopwatch di .

Si applica a

Vedi anche