Timer.Elapsed Zdarzenie

Definicja

Występuje, gdy interwał upłynął.

public:
 event System::Timers::ElapsedEventHandler ^ Elapsed;
public event System.Timers.ElapsedEventHandler Elapsed;
[System.Timers.TimersDescription("TimerIntervalElapsed")]
public event System.Timers.ElapsedEventHandler Elapsed;
member this.Elapsed : System.Timers.ElapsedEventHandler 
[<System.Timers.TimersDescription("TimerIntervalElapsed")>]
member this.Elapsed : System.Timers.ElapsedEventHandler 
Public Custom Event Elapsed As ElapsedEventHandler 

Typ zdarzenia

Atrybuty

Przykłady

Poniższy przykład tworzy wystąpienie Timer obiektu, który uruchamia zdarzenie Timer.Elapsed co dwie sekundy (2000 milisekund), konfiguruje procedurę obsługi zdarzeń dla zdarzenia i uruchamia czasomierz. Program obsługi zdarzeń wyświetla wartość ElapsedEventArgs.SignalTime właściwości za każdym razem, gdy jest wywoływana.

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
open System.Timers

let onTimedEvent source (e: ElapsedEventArgs) =
    printfn $"The Elapsed event was raised at {e.SignalTime}"

// Create a timer and set a two second interval.
let aTimer = new Timer()
aTimer.Interval <- 2000

// Hook up the Elapsed event for the timer. 
aTimer.Elapsed.AddHandler onTimedEvent

// Have the timer fire repeated events (true is the default)
aTimer.AutoReset <- true

// Start the timer
aTimer.Enabled <- true

printfn "Press the Enter key to exit the program at any time... "
stdin.ReadLine() |> ignore

// 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

Uwagi

Zdarzenie Elapsed jest zgłaszane, jeśli Enabled właściwość jest true i interwał czasu (w milisekundach) zdefiniowany przez Interval właściwość upłynął. AutoReset Jeśli właściwość ma truewartość , zdarzenie jest wywoływane wielokrotnie w interwale zdefiniowanym przez Interval właściwość. W przeciwnym razie zdarzenie jest wywoływane tylko raz, po raz pierwszy Interval upłynie wartość.

Jeśli Interval zostanie ustawiona po uruchomieniu Timer , liczba zostanie zresetowana. Jeśli na przykład ustawisz interwał na 5 sekund, a następnie ustawisz Enabled wartość true, liczba rozpoczyna się w czasie Enabled . Jeśli zresetujesz interwał do 10 sekund, gdy licznik wynosi 3 sekundy, Elapsed zdarzenie zostanie podniesione po raz pierwszy 13 sekund po Enabled ustawieniu wartości true.

SynchronizingObject Jeśli właściwość to null,Elapsedzdarzenie jest wywoływane w wątkuThreadPool. Jeśli przetwarzanie Elapsed zdarzenia trwa dłużej niż Interval, zdarzenie może zostać ponownie podniesione w innym ThreadPool wątku. W takiej sytuacji procedura obsługi zdarzeń powinna być ponownie dostępna.

Uwaga

Metoda obsługi zdarzeń może być uruchamiana w jednym wątku w tym samym czasie, gdy inny wątek wywołuje metodę Stop lub ustawia Enabled właściwość na false. Może to spowodować Elapsed podniesienie zdarzenia po zatrzymaniu czasomierza. Przykładowy kod Stop metody przedstawia jeden ze sposobów uniknięcia tego warunku wyścigu.

Nawet jeśli SynchronizingObject nie nullElapsed , zdarzenia mogą wystąpić po Dispose wywołaniu metody lub Stop lub po Enabled ustawieniu właściwości na falsewartość , ponieważ sygnał do podniesienia Elapsed zdarzenia jest zawsze w kolejce do wykonania w wątku puli wątków. Jednym ze sposobów rozwiązania tego warunku wyścigu jest ustawienie flagi, która nakazuje programowi obsługi Elapsed zdarzeń ignorowanie kolejnych zdarzeń.

Składnik Timer przechwytuje i pomija wszystkie wyjątki zgłaszane przez programy obsługi zdarzeń Elapsed dla zdarzenia. To zachowanie może ulec zmianie w przyszłych wersjach .NET Framework.

Dotyczy

Zobacz też