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

  • Home
  • SEARCH
  • 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 6892321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:31:14+00:00 2026-05-27T06:31:14+00:00

This question is a follow up to the C# Events and Thread Safety question

  • 0

This question is a follow up to the C# Events and Thread Safety question (I am not the author of that one) and the related blog post by Eric Lippert Events and Races. There are other similar questions on SO, but none of them actually consider this case, general consensus is that so long as you unsubscribe you are safe, but I don’t believe that is true all the time.

According to the discussion in both the SO question and the blog, the pattern that should be used is something like:

var ev = NotifyPropertyChanged;
if (ev != null)
    ev(this, new PropertyChangedEventArgs("Foo"));

But what if the following situation occurs:
1) I subscribe a listener:

mytype.NotifyPropertyChanged += Handler; // Handler is instance method in SomeObject class

2) I (or the run-time, due to scoping) dispose SomeObject, which contains the listener and unsubscribe the listener, at about the same time the property notification happens.

3) While unlikely due to very short time period this can happen in, it is theoretically possible that because ev preserves the old subscriber that is no longer in existence, it will call a function in an object that no longer exist.

According to the Eric Lippert, “event handlers are required to be robust in the face of being called even after the event has been unsubscribed“. But if the handler is unsubscribed and disposed, it can no longer take care of the call. What is the correct way to handle this situation?

Wrap the code from (1) in try-catch? What exception should be caught? ObjectDisposedException seems likely, but not the only one that can happen, I think.

  • 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-27T06:31:15+00:00Added an answer on May 27, 2026 at 6:31 am

    I believe that you meant to say an object that has already been GC’d, not disposed. Well that can’t happen; the MultiCastDelegate (the EventHandler) maintains a reference to the object via its subscribed method, i.e., it can’t be GC’d until the handler is removed.


    Dispose() has nothing to do with a method not being available, it is a pattern used to cleanup native resources, i.e., resources that cannot be handled by the GC.

    The object itself is still alive and well, though it may throw an exception if you call a method that depended upon that native resource (depends on implementation of course. The point is that the object still exists as does the method).

    There is nothing magical happening when you call Dispose(). I could easily whip up a class that implements IDisposable and has a completely empty Dispose() method. Call it all you will, it does nothing and does not change the state of the object in any way.

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

Sidebar

Related Questions

This question is a follow on from this one ... I am binding to
This question is a follow on from the following: UITouch - Event not responding
This is a follow-on question to Is publishing/subscribing to events after UI rendering a
This is a follow-up question to a different question I asked not too long
This is a follow-up question of my previous post . I am trying to
this question relates to performance penalities that may or may not arise from having
Not sure if this question belongs on stackoverflow.... I'm trying to follow along with
This question is a follow up with a previous question Previous Question The previous
This is a follow-up question to this question I asked earlier. Btw thanks Neil
I have a follow up question to this question . Is it possible to

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.