Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4088214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:57:12+00:00 2026-05-20T18:57:12+00:00

How do I avoid an event from being handled twice (if is the same

  • 0

How do I avoid an event from being handled twice (if is the same handler?)

Module Module1
  Sub Main()
    Dim item As New Item
    AddHandler item.TitleChanged, AddressOf Item_TitleChanged
    AddHandler item.TitleChanged, AddressOf Item_TitleChanged

    item.Title = "asdf"

    Stop
  End Sub
  Private Sub Item_TitleChanged(sender As Object, e As EventArgs)
    Console.WriteLine("Title changed!")
  End Sub
End Module

Public Class Item
  Private m_Title As String
  Public Property Title() As String
    Get
      Return m_Title
    End Get
    Set(ByVal value As String)
      m_Title = value
      RaiseEvent TitleChanged(Me, EventArgs.Empty)
    End Set
  End Property

  Public Event TitleChanged As EventHandler
End Class

Output:

Title changed!
Title changed!

Desired output:

Title changed!

I want the event manager to detect that this event is already handled by this handler and so it shouldn’t rehandle (or readd) it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:57:13+00:00Added an answer on May 20, 2026 at 6:57 pm

    Wrapping the event handler list in a HashSet will make sure the handlers are not duplicate references, the following snippet replacement for the question’s Item class will work under the above sample (it won’t re-add the handler if it’s already in the HashSet):

    Public Class Item
      Private m_Title As String
      Public Property Title() As String
        Get
          Return m_Title
        End Get
        Set(ByVal value As String)
          m_Title = value
          RaiseEvent TitleChanged(Me, EventArgs.Empty)
        End Set
      End Property
    
      Private handlers As New HashSet(Of EventHandler)
    
      Public Custom Event TitleChanged As EventHandler
        AddHandler(value As EventHandler)
          handlers.Add(value)
        End AddHandler
    
        RemoveHandler(value As EventHandler)
          handlers.Remove(value)
        End RemoveHandler
    
        RaiseEvent(sender As Object, e As System.EventArgs)
          For Each handler In handlers.Where(Function(h) h IsNot Nothing)
            handler(sender, e)
          Next
        End RaiseEvent
      End Event
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to avoid serializing an Event class member because when the event is
I'm trying to avoid code like this when reusing the same ViewUserControl in ASP.NET
I need to avoid being vulnerable to SQL injection in my ASP.NET application. How
I am trying to capture worksheets being copied in to a workbook from another
I'm quite new to C# (coming from a Java background) and I'm working on
The Prototype event listener I use for changes in select menus is not being
How do I avoid read locks in my database? Answers for multiple databases welcome!
After trying to avoid JavaScript for years, Iv started using Query for validation in
Should developers avoid using continue in C# or its equivalent in other languages to
If I avoid referencing assemblies that don't exist in the silverlight 2.0 runtime, will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.