Timer.Elapsed Evento

Definizione

Si verifica quando l'intervallo scade.Occurs when the interval elapses.

public:
 event System::Timers::ElapsedEventHandler ^ Elapsed;
[System.Timers.TimersDescription("Occurs when the Interval has elapsed.")]
[System.Timers.TimersDescription("TimerIntervalElapsed")]
public event System.Timers.ElapsedEventHandler Elapsed;
member this.Elapsed : System.Timers.ElapsedEventHandler 
Public Custom Event Elapsed As ElapsedEventHandler 
Attributi

Esempi

Nell'esempio seguente viene creata un'istanza di un oggetto Timer che genera l'evento Timer.Elapsed ogni due secondi (2000 millisecondi), imposta un gestore eventi per l'evento e avvia il timer.The following example instantiates a Timer object that fires its Timer.Elapsed event every two seconds (2000 milliseconds), sets up an event handler for the event, and starts the timer. Il gestore eventi Visualizza il valore della proprietà ElapsedEventArgs.SignalTime ogni volta che viene generato.The event handler displays the value of the ElapsedEventArgs.SignalTime property each time it is raised.

using namespace System;
using namespace System::Timers;

public ref class Example
{
private:
    static System::Timers::Timer^ aTimer;

public:
    static void Demo()
    {
        // Create a timer and set a two second interval.
        aTimer = gcnew System::Timers::Timer();
        aTimer->Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler(Example::OnTimedEvent);

        // Have the timer fire repeated events (true is the default)
        aTimer->AutoReset = true;

        // Start the timer
        aTimer->Enabled = true;

        Console::WriteLine("Press the Enter key to exit the program at any time... ");
        Console::ReadLine();
    }

private:
    static void OnTimedEvent(Object^ source, System::Timers::ElapsedEventArgs^ e)
    {
        Console::WriteLine("The Elapsed event was raised at {0}", e->SignalTime);
    }
};

int main()
{
    Example::Demo();
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM 

using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM 
Imports System.Timers

Public Module Example
    Private aTimer As Timer

    Public Sub Main()
        ' Create a timer and set a two second interval.
        aTimer = New System.Timers.Timer()
        aTimer.Interval = 2000

        ' Hook up the Elapsed event for the timer.  
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        ' Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = True

        ' Start the timer
        aTimer.Enabled = True

        Console.WriteLine("Press the Enter key to exit the program at any time... ")
        Console.ReadLine()
    End Sub

    Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
    End Sub
End Module
' The example displays output like the following: 
'       Press the Enter key to exit the program at any time... 
'       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:06 PM 

Commenti

L'evento Elapsed viene generato se la proprietà Enabled è true e l'intervallo di tempo (in millisecondi) definito dalla proprietà Interval è scaduto.The Elapsed event is raised if the Enabled property is true and the time interval (in milliseconds) defined by the Interval property elapses. Se la proprietà AutoReset è true, l'evento viene generato ripetutamente a un intervallo definito dalla proprietà Interval; in caso contrario, l'evento viene generato una sola volta, la prima volta che il valore Interval scade.If the AutoReset property is true, the event is raised repeatedly at an interval defined by the Interval property; otherwise, the event is raised only once, the first time the Interval value elapses.

Se Interval viene impostato dopo l'avvio del Timer, il conteggio viene reimpostato.If Interval is set after the Timer has started, the count is reset. Se, ad esempio, si imposta l'intervallo su 5 secondi e quindi si imposta Enabled su true, il conteggio inizia al momento in cui Enabled è impostato.For example, if you set the interval to 5 seconds and then set Enabled to true, the count starts at the time Enabled is set. Se l'intervallo viene reimpostato su 10 secondi quando count è 3 secondi, l'evento Elapsed viene generato per la prima volta 13 secondi dopo l'impostazione di Enabled su true.If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

Se la proprietà SynchronizingObject è null, il Elapsed evento viene generato in un thread ThreadPool.If the SynchronizingObject property is null,the Elapsed event is raised on a ThreadPool thread. Se l'elaborazione dell'evento Elapsed dura più di Interval, è possibile che l'evento venga generato nuovamente in un altro thread di ThreadPool.If the processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In questa situazione, il gestore eventi deve essere rientrante.In this situation, the event handler should be reentrant.

Nota

Il metodo di gestione degli eventi può essere eseguito in un thread nello stesso momento in cui un altro thread chiama il metodo Stop o imposta la proprietà Enabled su false.The event-handling method might run on one thread at the same time that another thread calls the Stop method or sets the Enabled property to false. Questo potrebbe comportare la generazione dell'evento Elapsed dopo l'arresto del timer.This might result in the Elapsed event being raised after the timer is stopped. Il codice di esempio per il metodo Stop illustra un modo per evitare questo race condition.The example code for the Stop method shows one way to avoid this race condition.

Anche se SynchronizingObject non è null, Elapsed eventi possono verificarsi dopo che è stato chiamato il metodo Dispose o Stop o dopo che la proprietà Enabled è stata impostata su false, perché il segnale per generare l'evento Elapsed viene sempre accodato per l'esecuzione in un thread del pool di thread.Even if SynchronizingObject is not null, Elapsed events can occur after the Dispose or Stop method has been called or after the Enabled property has been set to false, because the signal to raise the Elapsed event is always queued for execution on a thread pool thread. Un modo per risolvere questo race condition consiste nell'impostare un flag che indichi al gestore eventi dell'evento Elapsed di ignorare gli eventi successivi.One way to resolve this race condition is to set a flag that tells the event handler for the Elapsed event to ignore subsequent events.

Il componente Timer intercetta ed Elimina tutte le eccezioni generate dai gestori eventi per l'evento Elapsed.The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. Questo comportamento è soggetto a modifiche nelle versioni future del .NET Framework.This behavior is subject to change in future releases of the .NET Framework.

Si applica a

Vedi anche