.NET Framework Class Library
EventHandlerTEventArgs Class

Represents the method that will handle an event when the event provides data.

Inheritance Hierarchy
SystemObject
   SystemDelegate
     SystemMulticastDelegate
      SystemEventHandlerTEventArgs

Namespace:   System
Assemblies:   System.Runtime (in System.Runtime.dll)
  mscorlib (in mscorlib.dll)
Syntax
Public NotInheritable Class EventHandler(Of TEventArgs) _
	Inherits [%$TOPIC/db0etb8x_en-us_VS_110_2_0_0_0_0%]
public sealed class EventHandler<TEventArgs> : [%$TOPIC/db0etb8x_en-us_VS_110_2_0_1_0_0%]
generic<typename TEventArgs>
public ref class EventHandler sealed : public [%$TOPIC/db0etb8x_en-us_VS_110_2_0_2_0_0%]
[<[%$TOPIC/db0etb8x_en-us_VS_110_2_0_3_0_0%]>]
type EventHandler<'TEventArgs> =  
    class 
        inherit [%$TOPIC/db0etb8x_en-us_VS_110_2_0_3_0_1%] 
    end

Type Parameters

TEventArgs

The type of the event data generated by the event.

The EventHandlerTEventArgs type exposes the following members.

Constructors
  NameDescription
Public method Supported in .NET for Windows Store apps EventHandlerTEventArgs
Top
Properties
  NameDescription
Public property Supported in .NET for Windows Store apps TargetGets the class instance on which the current delegate invokes the instance method. (Inherited from Delegate.)
Top
Methods
  NameDescription
Public method Supported in .NET for Windows Store apps BeginInvoke
Public method Supported in .NET for Windows Store apps DynamicInvokeDynamically invokes (late-bound) the method represented by the current delegate. (Inherited from Delegate.)
Public method Supported in .NET for Windows Store apps EndInvoke
Public method Supported in .NET for Windows Store apps EqualsDetermines whether this multicast delegate and the specified object are equal. (Inherited from MulticastDelegate.)
Public method Supported in .NET for Windows Store apps GetHashCodeReturns the hash code for this instance. (Inherited from MulticastDelegate.)
Public method Supported in .NET for Windows Store apps GetInvocationListReturns the invocation list of this multicast delegate, in invocation order. (Inherited from MulticastDelegate.)
Public method Supported in .NET for Windows Store apps GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method Supported in .NET for Windows Store apps Invoke
Public method Supported in .NET for Windows Store apps ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension Method Supported in .NET for Windows Store apps GetMethodInfoGets an object that represents the method represented by the specified delegate. (Defined by RuntimeReflectionExtensions.)
Top
Remarks

The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A delegate that refers to a method that provides the response to the event.

  • Optionally, a class that holds the event data, if the event provides data.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

The standard signature of an event handler delegate defines a method that does not return a value. This method's first parameter is of type Object and refers to the instance that raises the event. Its second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply the value of the EventArgsEmpty field. Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.

The EventHandlerTEventArgs delegate is a predefined delegate that represents an event handler method for an event that generates data. The advantage of using EventHandlerTEventArgs is that you do not need to code your own custom delegate if your event generates event data. You simply provide the type of the event data object as the generic parameter.

To associate the event with the method that will handle the event, 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 Handling and Raising Events.

Examples

The following example shows an event named ThresholdReached. The event is associated with an EventHandlerTEventArgs delegate.

Module Module1

    Sub Main()
        Dim c As Counter = New Counter(New Random().Next(10))
        AddHandler c.ThresholdReached, AddressOf c_ThresholdReached

        Console.WriteLine("press 'a' key to increase total")
        While Console.ReadKey(True).KeyChar = "a"
            Console.WriteLine("adding one")
            c.Add(1)
        End While 
    End Sub 

    Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs)
        Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached)
        Environment.Exit(0)
    End Sub 
End Module 

Class Counter
    Private threshold As Integer 
    Private total As Integer 

    Public Sub New(passedThreshold As Integer)
        threshold = passedThreshold
    End Sub 

    Public Sub Add(x As Integer)
        total = total + x
        If (total >= threshold) Then 
            Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs()
            args.Threshold = threshold
            args.TimeReached = DateTime.Now
            OnThresholdReached(args)
        End If 
    End Sub 

    Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs)
        RaiseEvent ThresholdReached(Me, e)
    End Sub 

    Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs)
End Class 

Class ThresholdReachedEventArgs
    Inherits EventArgs

    Public Property Threshold As Integer 
    Public Property TimeReached As DateTime
End Class
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Counter c = new Counter(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;

            Console.WriteLine("press 'a' key to increase total");
            while (Console.ReadKey(true).KeyChar == 'a')
            {
                Console.WriteLine("adding one");
                c.Add(1);
            }
        }

        static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
        {
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);
            Environment.Exit(0);
        }
    }

    class Counter
    {
        private int threshold;
        private int total;

        public Counter(int passedThreshold)
        {
            threshold = passedThreshold;
        }

        public void Add(int x)
        {
            total += x;
            if (total >= threshold)
            {
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.TimeReached = DateTime.Now;
                OnThresholdReached(args);
            }
        }

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
    }

    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
}
using namespace System;

public ref class ThresholdReachedEventArgs : public EventArgs
{
   public:
      property int Threshold;
      property DateTime TimeReached;
};

public ref class Counter
{
   private:
      int threshold;
      int total;

   public:
      Counter() {};

      Counter(int passedThreshold)
      {
         threshold = passedThreshold;
      }

      void Add(int x)
      {
          total += x;
          if (total >= threshold) {
             ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
             args->Threshold = threshold;
             args->TimeReached = DateTime::Now;
             OnThresholdReached(args);
          }
      }

      event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;

   protected:
      virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
      {
         ThresholdReached(this, e);
      }
};

public ref class SampleHandler
{
   public:
      static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
      {
         Console::WriteLine("The threshold of {0} was reached at {1}.",
                            e->Threshold,  e->TimeReached);
         Environment::Exit(0);
      }
};

void main()
{
   Counter^ c = gcnew Counter((gcnew Random())->Next(10));
   c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);

   Console::WriteLine("press 'a' key to increase total");
   while (Console::ReadKey(true).KeyChar == 'a') {
      Console::WriteLine("adding one");
      c->Add(1);
   }
}
Version Information

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.