J
Jim
Hi,
I'm working with someone else's code and have an issue with the
application design for registering and de-registering events.
In the existing code...
Events are registered as shown below. If the routine that owns this
code is executed unexpectedly more than once. Then there can be more
than one registration for the event.
blah.blahChanged += new EventHandler(this.blahButton_BlahChanged);
Also, the existing code uses the dispose event to deregister the events
as shown below.
public virtual void Dispose()
{
blah.blah-= new KeyEventHandler(blah);
blah.blah-= new KeyEventHandler(blah);
blah.blah-= new KeyEventHandler(blah);
}
Then the existing code forces a garbage collection using GC.Collect;
Is this a bad thing??? Is it ok to use the dispose method to deregister
events by forcing a garbage collection?
I prefer to use this method...
blah.blahChanged -= new EventHandler(this.blahButton_BlahChanged);
blah.blahChanged += new EventHandler(this.blahButton_BlahChanged);
it ensures that only one registration exists.
I deregister events in other locations. not in the dispose routines?
Comments?
I'm working with someone else's code and have an issue with the
application design for registering and de-registering events.
In the existing code...
Events are registered as shown below. If the routine that owns this
code is executed unexpectedly more than once. Then there can be more
than one registration for the event.
blah.blahChanged += new EventHandler(this.blahButton_BlahChanged);
Also, the existing code uses the dispose event to deregister the events
as shown below.
public virtual void Dispose()
{
blah.blah-= new KeyEventHandler(blah);
blah.blah-= new KeyEventHandler(blah);
blah.blah-= new KeyEventHandler(blah);
}
Then the existing code forces a garbage collection using GC.Collect;
Is this a bad thing??? Is it ok to use the dispose method to deregister
events by forcing a garbage collection?
I prefer to use this method...
blah.blahChanged -= new EventHandler(this.blahButton_BlahChanged);
blah.blahChanged += new EventHandler(this.blahButton_BlahChanged);
it ensures that only one registration exists.
I deregister events in other locations. not in the dispose routines?
Comments?