How to: Raise and Consume Events
The examples in this topic show how to work with events. They include examples of the EventHandler delegate, the EventHandler(Of TEventArgs) delegate, and a custom delegate, to illustrate events with and without data.
The examples use concepts described in the Handling and Raising Events article.
The first example shows how to raise and consume an event that doesn't have data. It contains a class named Counter that has an event named ThresholdReached. This event is raised when a counter value equals or exceeds a threshold value. The EventHandler delegate is associated with the event, because no event data is provided.
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 EventArgs) Console.WriteLine("The threshold was reached.") 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 OnThresholdReached(EventArgs.Empty) End If End Sub Protected Overridable Sub OnThresholdReached(e As EventArgs) RaiseEvent ThresholdReached(Me, e) End Sub Public Event ThresholdReached As EventHandler End Class
The next example shows how to raise and consume an event that provides data. The EventHandler(Of TEventArgs) delegate is associated with the event, and an instance of a custom event data object is provided.
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
The next example shows how to declare a delegate for an event. The delegate is named ThresholdReachedEventHandler. This is just an illustration. Typically, you do not have to declare a delegate for an event, because you can use either the EventHandler or the EventHandler(Of TEventArgs) delegate. You should declare a delegate only in rare scenarios, such as making your class available to legacy code that cannot use generics.
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 ThresholdReachedEventHandler End Class Public Class ThresholdReachedEventArgs Inherits EventArgs Public Property Threshold As Integer Public Property TimeReached As DateTime End Class Public Delegate Sub ThresholdReachedEventHandler(sender As Object, e As ThresholdReachedEventArgs)