5

I have a Class A...in it's constructor...I am assigning an anonymous function to Object_B's eventHandler.

How do I remove (unsubscribe) that from Dispose method of class A ?

Any help would be appreciated ! Thanks

Public Class A
{

public A()
 {

 B_Object.DataLoaded += (sender, e) =>
                {
                   Line 1
                   Line 2
                   Line 3
                   Line 4
                };
 }

Public override void Dispose()
{
  // How do I unsubscribe the above subscribed anonymous function ?
}
}
Relativity
  • 6,690
  • 22
  • 78
  • 128

3 Answers3

8

You can't, basically. Either move it into a method, or use a member variable to keep the delegate for later:

public class A : IDisposable
{
    private readonly EventHandler handler;

    public A()
    {
        handler = (sender, e) =>
        {
           Line 1
           Line 2
           Line 3
           Line 4
        };

        B_Object.DataLoaded += handler;
     }

     public override void Dispose()
     {
        B_Object.DataLoaded -= handler;
     }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So, if we didnt unsubscribe that...it will obviously introduce a memory leakage, right ? – Relativity Mar 16 '12 at 22:38
  • 1
    @Relativity: Not necessarily. You haven't really talked about `B_Object.DataLoaded` - if that's *actually* an instance event rather than a static event, then if the object is collected the event subscription won't matter any more. It all depends on context... – Jon Skeet Mar 16 '12 at 22:40
  • B_Object.DataLoaded is an instance event..not static event. Could you please clarify this : - "if the object is collected the event subscription won't matter " – Relativity Mar 16 '12 at 22:42
  • 2
    @Relativity: If object X subscribes to any events from object Y but does not unsubscribe, the dangling events will cause object X to stay in memory at least as long as object Y. If the memory lifetime of object Y will be much longer than the useful lifetime of object X, this can be a bad thing. If during the memory lifetime of object Y, many instances of object X will be created and abandoned, it can be very bad if not disastrous. If, however, Y will be eligible for collection when time X becomes useless, the dangling subscription will not keep X alive. – supercat Mar 19 '12 at 16:21
  • 1
    @Relativity: Even though dangling subscriptions are harmless in such cases, they're still icky. It really peeves me that neither vb.net nor C# provides a convenient means of unsubscribing all the events to which one is attached. – supercat Mar 19 '12 at 16:22
1

This is an alternative without using a handler variable.

Public Class A
{

 public A()
  {

    B_Object.DataLoaded += (sender, e) =>
                {
                   Line 1
                   Line 2
                   Line 3
                   Line 4
                };
  }

  Public override void Dispose()
  {
   if(B_Object.DataLoaded != null)
   {
     B_Object.DataLoaded -=
         (YourDelegateType)B_Object.DataLoaded.GetInvocationList().Last();
       //if you are not sure that the last method is yours than you can keep an index
       //which is set in your ctor ...
   }
  }
 }
Ion Sapoval
  • 635
  • 5
  • 8
0

The right way to do this is to use the Rx Extensions. Go watch the videos here:

http://msdn.microsoft.com/en-us/data/gg577611

I found the "blues" movie particularly useful.

Brannon
  • 5,324
  • 4
  • 35
  • 83