How to: Declare Events That Conserve Memory Use

There are several circumstances when it is important that an application keep its memory usage low. Custom events allow the application to use memory only for the events that it handles.

By default, when a class declares an event, the compiler allocates memory for a field to store the event information. If a class has many unused events, they needlessly take up memory.

Instead of using the default implementation of events that Visual Basic provides, you can use custom events to manage the memory usage more carefully.

Example

In this example, the class uses one instance of the EventHandlerList class, stored in the Events field, to store information about the events in use. The EventHandlerList class is an optimized list class designed to hold delegates.

All events in the class use the Events field to keep track of what methods are handling each event.

Public Class MemoryOptimizedBaseControl
    ' Define a delegate store for all event handlers. 
    Private Events As New System.ComponentModel.EventHandlerList

    ' Define the Click event to use the delegate store. 
    Public Custom Event Click As EventHandler
        AddHandler(ByVal value As EventHandler)
            Events.AddHandler("ClickEvent", value)
        End AddHandler 
        RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler("ClickEvent", value)
        End RemoveHandler 
        RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            CType(Events("ClickEvent"), EventHandler).Invoke(sender, e)
        End RaiseEvent 
    End Event 

    ' Define the Click event to use the same delegate store. 
    Public Custom Event DoubleClick As EventHandler
        AddHandler(ByVal value As EventHandler)
            Events.AddHandler("DoubleClickEvent", value)
        End AddHandler 
        RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler("DoubleClickEvent", value)
        End RemoveHandler 
        RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            CType(Events("DoubleClickEvent"), EventHandler).Invoke(sender, e)
        End RaiseEvent 
    End Event 

    ' Define additional events to use the same delegate store. 
    ' ... 
End Class
Public Class MemoryOptimizedBaseControl
    ' Define a delegate store for all event handlers. 
    Private Events As New System.ComponentModel.EventHandlerList

    ' Define the Click event to use the delegate store. 
    Public Custom Event Click As EventHandler
        AddHandler(ByVal value As EventHandler)
            Events.AddHandler("ClickEvent", value)
        End AddHandler 
        RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler("ClickEvent", value)
        End RemoveHandler 
        RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            CType(Events("ClickEvent"), EventHandler).Invoke(sender, e)
        End RaiseEvent 
    End Event 

    ' Define the Click event to use the same delegate store. 
    Public Custom Event DoubleClick As EventHandler
        AddHandler(ByVal value As EventHandler)
            Events.AddHandler("DoubleClickEvent", value)
        End AddHandler 
        RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler("DoubleClickEvent", value)
        End RemoveHandler 
        RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            CType(Events("DoubleClickEvent"), EventHandler).Invoke(sender, e)
        End RaiseEvent 
    End Event 

    ' Define additional events to use the same delegate store. 
    ' ... 
End Class

See Also

Tasks

How to: Declare Events That Avoid Blocking

Reference

Event Statement

EventHandlerList