C# listening to 3rd party long living event using weak reference -


i attaching 3rd party, long living deleted event publisher, ends keeping possibly short lived objects alive due event handler. deleted event never triggered, have handle things if is. it's not obvious un-subscribe deleted event, weak reference objects can gc'd.

i have seen lot of elaborate ways create weak event handlers, following snippet seems trick, @ least in provided test snippet. insane or can work?

(http://diditwith.net/commentview,guid,aacdb8ae-7baa-4423-a953-c18c1c7940ab.aspx says under "a first stab" similar snippet "(...) isn't robust enough used event (...)", why not?)

public static class weakevent {     private class weakeventholder<targs> targs : eventargs     {         private readonly weakreference _handler;          public weakeventholder(action<object, targs> handler)         {             _handler = new weakreference(handler);         }          public void handle(object sender, targs args)         {             action<object, targs> handler = (action<object, targs>)_handler.target;             if (handler != null)                 handler(sender, args);         }     }      public static eventhandler makehandler(action<object, eventargs> handler)     {         return new weakeventholder<eventargs>(handler).handle;     } } 

test class

[testfixture] public class tests {     public class publisher     {         public eventhandler event;          public void raise()         {             if (event != null)                 event(this, eventargs.empty);         }     }      public class target     {         public target(publisher publisher)         {             publisher.event += weakevent.makehandler(handleevent);         }          public void handleevent(object sender, eventargs args)         {             system.diagnostics.trace.writeline("handleevent");         }     }      [test]     public void test()     {         publisher publisher = new publisher();         weakreference wref = new weakreference(new target(publisher));         gc.collect();          publisher.raise();          assert.false(wref.isalive);     } } 

because action<object, targs> handler maybe garbage collected before it's target is. here's unit test exposes problem:

public class bar {     public void foo(object sender, eventargs args)     {     } }  [test] public void actionisnotgcedbeforetarget() {     bar bar = new bar();     action<object, eventargs> action = bar.foo;     weakreference weakref = new weakreference(action);     action = null;     gc.collect();      assert.istrue(weakref.isalive); // false } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -