ElapsedEventHandler-Delegat
Stellt die Methode dar, die das Elapsed-Ereignis von Timer behandelt.
Namespace: System.Timers
Assembly: System (in system.dll)
Syntax
'Declaration
Public Delegate Sub ElapsedEventHandler ( _
sender As Object, _
e As ElapsedEventArgs _
)
'Usage
Dim instance As New ElapsedEventHandler(AddressOf HandlerMethod)
public delegate void ElapsedEventHandler (
Object sender,
ElapsedEventArgs e
)
public delegate void ElapsedEventHandler (
Object^ sender,
ElapsedEventArgs^ e
)
/** @delegate */
public delegate void ElapsedEventHandler (
Object sender,
ElapsedEventArgs e
)
JScript unterstützt die Verwendung von Delegaten, aber nicht die Deklaration von neuen Delegaten.
Parameter
- sender
Die Quelle des Ereignisses.
- e
Ein ElapsedEventArgs-Objekt, das die Ereignisdaten enthält.
Hinweise
Beim Erstellen eines ElapsedEventHandler-Delegaten geben Sie die Methode für die Ereignisbehandlung an. Um das Ereignis mit dem Ereignishandler zu verknüpfen, fügen Sie dem Ereignis eine Instanz des Delegaten hinzu. Der Ereignishandler wird bei jedem Eintreten des Ereignisses aufgerufen, sofern der Delegat nicht entfernt wird. Weitere Informationen über Ereignishandlerdelegaten finden Sie unter Ereignisse und Delegaten.
Beispiel
Imports System
Imports System.Timers
Public Class Timer1
Public Shared Sub Main()
' Normally, the timer is declared at the class level, so
' that it doesn't go out of scope when the method ends.
' In this example, the timer is needed only while Main
' is executing. However, KeepAlive must be used at the
' end of Main, to prevent the JIT compiler from allowing
' aggressive garbage collection to occur before Main
' ends.
Dim aTimer As New System.Timers.Timer()
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine()
' Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer)
End Sub
' Specify what you want to happen when the Elapsed event is
' raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class
using System;
using System.Timers;
public class Timer1
{
public static void Main()
{
// Normally, the timer is declared at the class level, so
// that it doesn't go out of scope when the method ends.
// In this example, the timer is needed only while Main
// is executing. However, KeepAlive must be used at the
// end of Main, to prevent the JIT compiler from allowing
// aggressive garbage collection to occur before Main
// ends.
System.Timers.Timer aTimer = new System.Timers.Timer();
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
#using <system.dll>
using namespace System;
using namespace System::Timers;
public ref class Timer1
{
public:
static void Demo()
{
// Normally, the timer is declared at the class level, so
// that it doesn't go out of scope when the method ends.
// In this example, the timer is needed only while Demo
// is executing. However, KeepAlive must be used at the
// end of Demo, to prevent the JIT compiler from allowing
// aggressive garbage collection to occur before Demo
// ends.
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer;
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent );
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Interval = 2000;
aTimer->Enabled = true;
Console::WriteLine("Press the Enter key to exit the program.");
Console::ReadLine();
// Keep the timer alive until the end of the Demo method.
GC::KeepAlive(aTimer);
}
private:
// Specify what you want to happen when the Elapsed event is
// raised.
static void OnTimedEvent( Object^ /*source*/, ElapsedEventArgs^ /*e*/ )
{
Console::WriteLine( "Hello World!" );
}
};
int main()
{
Timer1::Demo();
}
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0