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 7819281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:52:59+00:00 2026-06-02T06:52:59+00:00

If you subscribe the .net event with the same subscribe more then once, then

  • 0

If you subscribe the .net event with the same subscribe more then once, then your subscribed method will be called the same times as subscribed. And if you unsubscribe just once, then it will be just one minus to the call. Meaning you have to unsubscribe same no of times as subscribed, otherwise u will be keep informed. Somtimes you don’t want to do it.

In order to to prevent an event Handler to be hooked twice, we can implement event as following.

private EventHandler foo;
public event EventHandler Foo
{
    add
    {
        if( foo == null || !foo.GetInvocationList().Contains(value) )
        {
            foo += value;
        }
    }
    remove
    {
        foo -= value;
    }
}

Now I would like to implement Postsharp EventInterceptionAspect to make this solution generic, so that I can apply PreventEventHookedTwiceAttribute on every event to save lot of code. But I can’t figure out how to check the second part of the following condition in add. I mean foo.GetInvocationList().Contains(value). My PreventEventHookedTwiceAttribute looks like following.

[Serializable]
public class PreventEventHookedTwiceAttribute: EventInterceptionAspect
{
    public override void OnAddHandler(EventInterceptionArgs args)
    {
        if(args.Event == null || secondConditionRequired) // secondConditionRequired means it is required.            
        {
            args.ProceedAddHandler();
        }
    }
}

I don’t need to override OnRemoveHandler, as default functionality is enough here.

  • 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-06-02T06:53:02+00:00Added an answer on June 2, 2026 at 6:53 am

    This class does the trick.

    [Serializable]
    public class PreventEventHookedTwiceAttribute: EventInterceptionAspect
    {
        private readonly object _lockObject = new object();
        readonly List<Delegate> _delegates = new List<Delegate>();
    
        public override void OnAddHandler(EventInterceptionArgs args)
        {
            lock(_lockObject)
            {
                if(!_delegates.Contains(args.Handler))
                {
                    _delegates.Add(args.Handler);
                    args.ProceedAddHandler();
                }
            }
        }
    
        public override void OnRemoveHandler(EventInterceptionArgs args)
        {
            lock(_lockObject)
            {
                if(_delegates.Contains(args.Handler))
                {
                    _delegates.Remove(args.Handler);
                    args.ProceedRemoveHandler();
                }
            }
        }
    }
    

    Example Usage to display the difference is given as below.

    class Program
        {
            private static readonly object _lockObject = new object();
            private static int _counter = 1;
    
            [PreventEventHookedTwice]
            public static event Action<string> GoodEvent;
    
    
            public static event Action<string> BadEvent;
    
            public static void Handler (string message)
            {
                lock(_lockObject)
                {
                    Console.WriteLine(_counter +": "+ message);
                    _counter++;
                }
            }
    
            static void Main(string[] args)
            {
                GoodEvent += Handler;
                GoodEvent += Handler;
                GoodEvent += Handler;
                GoodEvent += Handler;
                GoodEvent += Handler;
                Console.WriteLine("Firing Good Event. Good Event is subscribed 5 times from the same Handler.");
                GoodEvent("Good Event is Invoked.");
    
                _counter = 1;
                BadEvent += Handler;
                BadEvent += Handler;
                BadEvent += Handler;
                BadEvent += Handler;
                BadEvent += Handler;
                Console.WriteLine("Firing Bad Event. Bad Event is subscribed 5 times from the same Handler.");
                BadEvent("Bad Event is Invoked.");
    
                _counter = 1;
                GoodEvent -= Handler;
                Console.WriteLine("GoodEvent is unsubscribed just once. Now fire the Event");
                if(GoodEvent!= null)
                {
                    GoodEvent("Good Event Fired");
                }
                Console.WriteLine("Event is not received to Handler.");
    
                BadEvent -= Handler;
                Console.WriteLine("BadEvent is unsubscribed just once. Now fire the Event");
                BadEvent("Good Event Fired");
                Console.WriteLine("Event is fired 4 times. If u subscribe good event 5 times then u have to unscribe it for 5 times, otherwise u will be keep informed.");
    
                Console.ReadLine();
            }
        }
    

    Postsharp Rocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you subscribe to an event in .NET, the subscription is added to a
I would like to subscribe to whatever event is fired when my ASP.NET form
Using PowerShell, it is possible to subscribe to a .NET, WMI or engine event
I'm writing a topic-based publish/subscribe system in C++. My generic Event class has three
Is there currently a way to subscribe to an event of a wrapped set,
Is there a way to subscribe to the HttpApplication's BeginRequest event from Global.asax? What
I was going through an article on event bubbling in asp.net and came to
I am trying to trigger the FB subscribe event, but the alert does not
Style question. The C# 1.0 way to subscribe to an event is to use
Once the problem of loading plugins is solved (in .NET through MEF in out

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.