Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deregister my Lambda callback?

If I create a lambda callback such as this:

var someInstance = new SomeObject();

someInstance.Finished += (obj, args) =>
{
      // Do something

      // Deregister here
};

someInstance.DoAction();

How could I deregister the callback as part of the actual handler code? This pattern would be ideal as I could ensure it is released as soon as possible.

I've seen similar questions but not one where this type of example is directly addressed.

like image 682
Keith Adler Avatar asked Apr 26 '11 22:04

Keith Adler


1 Answers

With something like,

var someInstance = new SomeObject();

EventHandler myDelegate = null;
myDelegate = (obj, args) =>
{
      // Do something

      // Deregister here
      someInstance.Finished -= myDelegate;
};
someInstance.Finished += myDelegate;

someInstance.DoAction();
like image 138
Chuck Savage Avatar answered Oct 04 '22 22:10

Chuck Savage