Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Is event subscription/unsubscription thread-safe?

Is event subscription/unsubscription thread-safe?

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharplinuxquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    supercat9
    wrote on last edited by
    #1

    My understanding of event handling in .net is that event subscription and unsubscription are performed by m_theEvent[delegate].Combine(m_theEvent, subscribingDelegate) or m_theEvent[delegate].Remove(m_theEvent, unsubscribingDelegate). Certainly that is how they are illustrated in the books I've read. Such constructs would not be threadsafe, but when I've written some code to bash events (I only have a single-processor) I've not seen any subscription failures though I have seen unsubscription failures. Do .net 2.0 and above do anything to ensure that subscriptions actually succeed (e.g. via locks or a CompareExchange loop), or is the code just getting "lucky", or what? If multiple threads will be subscribing and unsubscribing, may events safely be used or should some other mechanism be used instead?

    G 1 Reply Last reply
    0
    • S supercat9

      My understanding of event handling in .net is that event subscription and unsubscription are performed by m_theEvent[delegate].Combine(m_theEvent, subscribingDelegate) or m_theEvent[delegate].Remove(m_theEvent, unsubscribingDelegate). Certainly that is how they are illustrated in the books I've read. Such constructs would not be threadsafe, but when I've written some code to bash events (I only have a single-processor) I've not seen any subscription failures though I have seen unsubscription failures. Do .net 2.0 and above do anything to ensure that subscriptions actually succeed (e.g. via locks or a CompareExchange loop), or is the code just getting "lucky", or what? If multiple threads will be subscribing and unsubscribing, may events safely be used or should some other mechanism be used instead?

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      This might help: Event Declaration Syntax[^]

      Giorgi Dalakishvili #region signature my articles #endregion

      S 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        This might help: Event Declaration Syntax[^]

        Giorgi Dalakishvili #region signature my articles #endregion

        S Offline
        S Offline
        supercat9
        wrote on last edited by
        #3

        I'm using vb.net, rather than c#, but I think I understand most of the article. A few things I'm not quite clear on, though: -1- If I declare a custom event, will all subscribers to the event use the subscribe/unsubscribe methods described therefor, and make no other access to anything to do with the event? -2- What happens to event handlers for an object that falls out of scope? If I have an object that contains a reference to another object that sends events, will the presence of the former object in the latter object's subscription list keep it alive indefinitely? -3- Is there any way to have an event subscriber keep or pass something other than a raw delegate? My guess is probably not when using the standard mechanisms, but if a subscriber could hold some data which could be passed with an unsubscribe request, efficient lock-free thread-safe subscription management would be practical.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups