Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ElapsedEventHandler Delegate

ElapsedEventHandler Delegate

Represents the method that will handle the Elapsed event of a Timer.

[Visual Basic]
<Serializable>
Public Delegate Sub ElapsedEventHandler( _
   ByVal sender As Object, _
   ByVal e As ElapsedEventArgs _
)
[C#]
[Serializable]
public delegate void ElapsedEventHandler(
   object sender,
   ElapsedEventArgs e
);
[C++]
[Serializable]
public __gc __delegate void ElapsedEventHandler(
   Object* sender,
   ElapsedEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the ElapsedEventHandler delegate declaration.

sender
The source of the event.
e
An ElapsedEventArgs object that contains the event data.

Remarks

When you create an ElapsedEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

Example

[Visual Basic] 
Public Class Timer1
    
    Public Shared Sub Main()
        Dim aTimer As New System.Timers.Timer()
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        ' Set the Interval to 5 seconds.
        aTimer.Interval = 5000
        aTimer.Enabled = True
        
        Console.WriteLine("Press 'q' to quit the sample.")
        While Console.Read() <> CInt("q")
        End While
    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


[C#] 
public class Timer1
 {
 
     public static void Main()
     {
         System.Timers.Timer aTimer = new System.Timers.Timer();
         aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
         // Set the Interval to 5 seconds.
         aTimer.Interval=5000;
         aTimer.Enabled=true;
 
         Console.WriteLine("Press \'q\' to quit the sample.");
         while(Console.Read()!='q');
     }
 
     // Specify what you want to happen when the Elapsed event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
         Console.WriteLine("Hello World!");
     }
 }
 

[C++] 
public __gc class Timer1
{
public:
    static void Main() {
        System::Timers::Timer* aTimer = new System::Timers::Timer;
        aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer->Interval=5000;
        aTimer->Enabled=true;
    }
private:
    // Specify what you want to happen when the Elapsed event is raised.
     static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
     {
         Console::WriteLine(S"Hello World!");
     }
};

int main()
{
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Timers

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

System.Timers Namespace | Elapsed | ElapsedEventArgs

Show:
© 2015 Microsoft