Anonymous Delegates and Events

  • Thread starter Thread starter msnews.microsoft.com
  • Start date Start date
M

msnews.microsoft.com

Once the form is activated I would like to remove the reference to the
anonymous delegate. Can I reference the delegate from within the delegate?
For example ...

form.Activated += delegate
{
wasActivated = true;
form.Activated -= ????
}


next time form is activeated, this shouldn't be called again.

Thoughts?
 
Why should it not be called again? At least in your method below you don't
provide
code that demonstrates why Activated can't be reentrant, so I'll offer the
following.
If you are looking for reentrant protection then you can use your activation
variable
to signal out of the event.

form.Activated += delegate {
if ( wasActivated ) { return; }
wasActivated = true;
// Do your processing
}

Who cares about removing the delegate right? If you really do need to remove
the
delegate later then you'll have to store some reference to it. There is a
reference to
the delegate within the EventHandlerList returned by Component.Events, however,
this collection is object key indexed and the object used to index the Activated
event
is private to the Form. That makes getting ahold of the actual Delegate list
quite hard.
 
Back
Top