Timer.Elapsed 事件

定义

达到间隔时发生。

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 ,并且时间间隔 () 属性定义的以毫秒为单位,则引发 事件IntervalAutoReset如果 属性为 true,则事件在属性定义的Interval时间间隔内重复引发;否则,事件仅引发一次,即第一次Interval使用值。

如果在 Interval 启动后 Timer 设置 ,则重置计数。 例如,如果将间隔设置为 5 秒,然后将 设置为 Enabledtrue,则会在设置时 Enabled 开始计数。 如果在 count 为 3 秒时将间隔重置为 10 秒,则会Elapsed在设置为 trueEnabled 13 秒首次引发事件。

SynchronizingObject如果 属性为 null,则会在Elapsed线程上ThreadPool引发 事件。 如果事件的处理 Elapsed 持续时间超过 Interval,则可能会在另一个 ThreadPool 线程上再次引发该事件。 在这种情况下,事件处理程序应可重入。

注意

事件处理方法可能会在另一个线程调用 Stop 该方法或将 Enabled 属性设置为 false的同时在一个线程上运行。 这可能会导致 Elapsed 在计时器停止后引发事件。 方法的示例代码 Stop 演示了避免此争用条件的一种方法。

即使 不是 ,事件也可能在调用 或 Stop 方法或 Dispose 属性设置为 falseEnabled发生,因为引发Elapsed事件的信号始终在线程池线程上排队等待执行。 ElapsednullSynchronizingObject 解决此争用条件的一种方法是设置一个标志,指示事件的事件处理程序 Elapsed 忽略后续事件。

组件 Timer 捕获并禁止事件事件处理程序 Elapsed 引发的所有异常。 此行为在 .NET Framework 的未来版本中可能会发生更改。

适用于

另请参阅