Timer.Elapsed 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於間隔耗盡時。
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
事件類型
- 屬性
範例
下列範例會具現化 Timer 物件,每隔兩秒 (2000 毫秒) 引發其 Timer.Elapsed 事件、設定事件的事件處理常式,然後啟動計時器。 事件處理常式會在每次引發時顯示 屬性的值 ElapsedEventArgs.SignalTime 。
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
備註
Elapsed如果 Enabled 屬性為 true
,且屬性所 Interval 定義的時間間隔 (以毫秒為單位,則會引發事件) 。
AutoReset如果 屬性為 true
,則會以 屬性定義的 Interval 間隔重複引發事件;否則,只會在第一次耗用值時 Interval 引發事件一次。
如果在 Interval 啟動之後 Timer 設定 ,則會重設計數。 例如,如果您將間隔設定為 5 秒,然後將 設定 Enabled 為 true
,則會在設定時間 Enabled 開始計數。 如果您在計數為 3 秒時將間隔重設為 10 秒,則會 Elapsed 在 設定為 之後 Enabledtrue
的第一次 13 秒引發事件。
SynchronizingObject如果 屬性為 null
,則會 Elapsed 在 ThreadPool 執行緒上引發 事件。 如果事件的處理 Elapsed 時間超過 Interval ,則事件可能會在另一個 ThreadPool 執行緒上再次引發。 在此情況下,事件處理常式應該重新進入。
注意
事件處理方法可能會在另一個執行緒呼叫 Stop 方法,或將 Enabled 屬性設定為 false
時,于一個執行緒上執行。 這可能會導致 Elapsed 在計時器停止之後引發事件。 方法的 Stop 範例程式碼示範一種方式,以避免這種競爭狀況。
即使 不是 ,事件也可能發生在 呼叫 或 Stop 方法之後 Dispose ,或 屬性設定為 false
之後 Enabled ,因為引發 Elapsed 事件的訊號一律排入佇列,以線上程集區執行緒上執行。 Elapsednull
SynchronizingObject 解決此競爭條件的其中一種方法是設定旗標,告知事件的事件處理常式 Elapsed 忽略後續事件。
元件 Timer 會攔截並隱藏事件事件處理常式 Elapsed 擲回的所有例外狀況。 未來版本的.NET Framework可能會變更此行為。