Timer.Elapsed Kejadian
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Terjadi ketika interval berlalu.
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
Jenis Acara
- Atribut
Contoh
Contoh berikut membuat instans Timer objek yang menembakkan peristiwanya Timer.Elapsed setiap dua detik (2000 milidetik), menyiapkan penanganan aktivitas untuk peristiwa tersebut, dan memulai timer. Penanganan aktivitas menampilkan nilai ElapsedEventArgs.SignalTime properti setiap kali dinaikkan.
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
Keterangan
Peristiwa Elapsed dinaikkan jika Enabled properti adalah true
dan interval waktu (dalam milidetik) yang ditentukan oleh Interval properti berlalu.
AutoReset Jika properti adalah true
, peristiwa dinaikkan berulang kali pada interval yang ditentukan oleh Interval properti; jika tidak, peristiwa hanya dinaikkan sekali, pertama kali Interval nilai berlalu.
Jika Interval diatur setelah Timer dimulai, hitungan diatur ulang. Misalnya, jika Anda mengatur interval ke 5 detik lalu mengatur Enabled ke true
, hitungan dimulai pada saat Enabled diatur. Jika Anda mengatur ulang interval menjadi 10 detik saat hitungan adalah 3 detik, Elapsed peristiwa dinaikkan untuk pertama kalinya 13 detik setelah Enabled diatur ke true
.
SynchronizingObject Jika properti adalah null
,Elapsedperistiwa dinaikkan pada ThreadPool utas. Jika pemrosesan Elapsed peristiwa berlangsung lebih lama dari Interval, peristiwa mungkin dinaikkan lagi di utas lain ThreadPool . Dalam situasi ini, penanganan aktivitas harus masuk kembali.
Catatan
Metode penanganan peristiwa mungkin berjalan pada satu utas pada saat yang sama ketika utas Stop lain memanggil metode atau mengatur Enabled properti ke false
. Ini dapat mengakibatkan Elapsed peristiwa dinaikkan setelah timer dihentikan. Contoh kode untuk metode menunjukkan Stop salah satu cara untuk menghindari kondisi balapan ini.
Bahkan jika SynchronizingObject bukan null
, Elapsed peristiwa dapat terjadi setelah Dispose metode atau Stop dipanggil atau setelah Enabled properti diatur ke false
, karena sinyal untuk menaikkan Elapsed peristiwa selalu diantrekan untuk dieksekusi pada utas kumpulan utas. Salah satu cara untuk mengatasi kondisi balapan ini adalah dengan mengatur bendera yang memberi tahu penanganan aktivitas agar Elapsed peristiwa mengabaikan peristiwa berikutnya.
Komponen Timer menangkap dan menekan semua pengecualian yang dilemparkan oleh penanganan aktivitas untuk peristiwa tersebut Elapsed . Perilaku ini dapat berubah dalam rilis .NET Framework mendatang.