Timer.Elapsed Olay

Tanım

Zaman aralığı geçtiğinde gerçekleşir.

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 

Olay Türü

Öznitelikler

Örnekler

Aşağıdaki örnek, iki saniyede bir Timer (2000 milisaniye) olayını başlatan Timer.Elapsed , olay için bir olay işleyicisi ayarlayan ve zamanlayıcıyı başlatan bir nesnenin örneğini oluşturur. Olay işleyicisi, her yükseltildiğinde özelliğinin ElapsedEventArgs.SignalTime değerini görüntüler.

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

Açıklamalar

Elapsed Özellik ise Enabled ve özellik true tarafından tanımlanan zaman aralığı (milisaniye cinsinden) geçtiğinde Interval olay tetiklenir. AutoReset özelliği isetrue, olay özelliği tarafından Interval tanımlanan bir aralıkta art arda oluşturulur; aksi takdirde, değer ilk kez Interval geçtiğinde olay yalnızca bir kez tetiklenir.

başlatıldıktan sonra Timer ayarlanırsaInterval, sayı sıfırlanır. Örneğin, aralığı 5 saniye olarak ayarlarsanız ve sonra olarak ayarlarsanız Enabledtrue, sayı o sırada Enabled başlar. Sayım 3 saniye olduğunda aralığı 10 saniyeye sıfırlarsanız, Elapsed olay olarak ayarlandıktan true13 saniye sonra Enabled ilk kez oluşturulur.

SynchronizingObject özelliği isenullElapsed, olay bir ThreadPool iş parçacığında oluşturulur. Olayın işlenmesi değerinden ElapsedIntervaldaha uzun sürüyorsa, olay başka bir ThreadPool iş parçacığında yeniden tetiklenebilir. Bu durumda, olay işleyicisi yeniden girişi olmalıdır.

Not

Olay işleme yöntemi, başka bir iş parçacığının yöntemini çağırması Stop veya özelliğini falseolarak ayarlaması ile aynı anda bir iş parçacığında Enabled çalışabilir. Bu, zamanlayıcı durdurulduktan sonra olayın tetiklenmesinin sonucunu Elapsed verebilir. yöntemi için örnek kod, bu yarış durumundan Stop kaçınmanın bir yolunu gösterir.

olmasa SynchronizingObjectnullbile, Elapsed olaylar veya Stop yöntemi çağrıldıktan sonra Dispose veya özelliği olarak ayarlandıktan falsesonra Enabled gerçekleşebilir çünkü olayı tetikleye Elapsed sinyal her zaman bir iş parçacığı havuzu iş parçacığında yürütülmek üzere kuyruğa alınmıştır. Bu yarış durumunu çözmenin bir yolu, olay işleyicisine Elapsed sonraki olayları yoksaymayı bildiren bir bayrak ayarlamaktır.

Bileşen, Timer olay işleyicileri tarafından olay için Elapsed oluşan tüm özel durumları yakalar ve bastırır. Bu davranış, .NET Framework gelecek sürümlerinde değiştirilebilir.

Şunlara uygulanır

Ayrıca bkz.