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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:26:58+00:00 2026-05-11T00:26:58+00:00

I would like to ensure that I only subscribe once in a particular class

  • 0

I would like to ensure that I only subscribe once in a particular class for an event on an instance.

For example I would like to be able to do the following:

if (*not already subscribed*) {     member.Event += new MemeberClass.Delegate(handler); } 

How would I go about implementing such a guard?

  • 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. 2026-05-11T00:26:58+00:00Added an answer on May 11, 2026 at 12:26 am

    If you are talking about an event on a class that you have access to the source for then you could place the guard in the event definition.

    private bool _eventHasSubscribers = false; private EventHandler<MyDelegateType> _myEvent;  public event EventHandler<MyDelegateType> MyEvent {    add     {       if (_myEvent == null)       {          _myEvent += value;       }    }    remove    {       _myEvent -= value;    } } 

    That would ensure that only one subscriber can subscribe to the event on this instance of the class that provides the event.

    EDIT please see comments about why the above code is a bad idea and not thread safe.

    If your problem is that a single instance of the client is subscribing more than once (and you need multiple subscribers) then the client code is going to need to handle that. So replace

    not already subscribed

    with a bool member of the client class that gets set when you subscribe for the event the first time.

    Edit (after accepted): Based on the comment from @Glen T (the submitter of the question) the code for the accepted solution he went with is in the client class:

    if (alreadySubscribedFlag) {     member.Event += new MemeberClass.Delegate(handler); } 

    Where alreadySubscribedFlag is a member variable in the client class that tracks first subscription to the specific event. People looking at the first code snippet here, please take note of @Rune’s comment – it is not a good idea to change the behavior of subscribing to an event in a non-obvious way.

    EDIT 31/7/2009: Please see comments from @Sam Saffron. As I already stated and Sam agrees the first method presented here is not a sensible way to modify the behavior of the event subscription. The consumers of the class need to know about its internal implementation to understand its behavior. Not very nice.
    @Sam Saffron also comments about thread safety. I’m assuming that he is referring to the possible race condition where two subscribers (close to) simultaneously attempt to subscribe and they may both end up subscribing. A lock could be used to improve this. If you are planning to change the way event subscription works then I advise that you read about how to make the subscription add/remove properties thread safe.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I think this may work. If anyone sees something I… May 11, 2026 at 12:39 pm
  • added an answer Byte 189 represents a '½' in iso-8859-1 (aka 'Latin-1'), so… May 11, 2026 at 12:39 pm
  • added an answer $mysqldate = date( 'Y-m-d H:i:s', $phpdate ); $phpdate = strtotime(… May 11, 2026 at 12:39 pm

Related Questions

I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,
I have created a custom WPF user control which is intended to be used
I have a utility (grep) that gives me a list of filenames and a
I would like to print only the contents of a textarea element from a
I have an instance of ActiveMQ running on an externally facing server and exposing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.