Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / February 2007

Tip: Looking for answers? Try searching our database.

How to remove event handler

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
moondaddy - 27 Feb 2007 20:29 GMT
I'm working in WPF and c# and am adding an event handler to an object like
this:

this.tgt.SizeChanged += new SizeChangedEventHandler(OnTargetSizeChanged);

Later I kill the instance of tgt like this:

tgt = null;;  //which I'm not sure this is the most correct way of getting
rid of it.

However, I find that  the event 'OnTargetSizeChanged' is still fireing.
Evedently the event handler is still alive and wired.  Before I kill the
object 'tgt', should I be removing any attached event handlers?  If so, how?

Thanks.

moondaddy@noemail.noemail
Jon Skeet [C# MVP] - 27 Feb 2007 20:53 GMT
> I'm working in WPF and c# and am adding an event handler to an object like
> this:
[quoted text clipped - 5 lines]
> tgt = null;;  //which I'm not sure this is the most correct way of getting
> rid of it.

That's not "killing" anything. It's making the object available to the
garbage collector *if* nothing else is referencing it. Is it still a
control on the form, perhaps?

> However, I find that  the event 'OnTargetSizeChanged' is still fireing.
> Evedently the event handler is still alive and wired.  Before I kill the
> object 'tgt', should I be removing any attached event handlers?  If
> so, how?

Well, I think you need to seriously consider how you're "removing" tgt,
but to remove the event handler, just do:

this.tgt.SizeChanged -= new SizeChangedEventHandler
                                         (OnTargetSizeChanged);

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Keith Patrick - 27 Feb 2007 22:01 GMT
Wouldn't that fail due to the SizeChangedEventHandler being a different
instance than was added via +=?
Jon Skeet [C# MVP] - 27 Feb 2007 22:25 GMT
> Wouldn't that fail due to the SizeChangedEventHandler being a different
> instance than was added via +=?

No. They'd still compare as being equal, which is what's used.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Keith Patrick - 27 Feb 2007 23:33 GMT
Well, cool!  I was gonna do the -= on some code a while back but saw the new
instance I had to create and didn't even check to see if it would work.
Guess I can finally fix that!
moondaddy - 28 Feb 2007 03:59 GMT
Thanks this looks like just what I need.  By the way, this code is running
in a class that manages some custom bindings and tgt is a reference to a
user control on a canvas.  when I connect a line to tgt, I use a custom
binding in this class.  When tgt becomes un-connected I unbind it from the
line and no longer need a reference to tgt so I set tgt=null in the binding
management class.  However, tgt is still alive and well in the canvas.
Saying all of this, is it appropriate to set tgt to null here?  or should I
do something else?

>> I'm working in WPF and c# and am adding an event handler to an object
>> like
[quoted text clipped - 22 lines]
> this.tgt.SizeChanged -= new SizeChangedEventHandler
>                                          (OnTargetSizeChanged);
Jon Skeet [C# MVP] - 28 Feb 2007 07:30 GMT
> Thanks this looks like just what I need.  By the way, this code is running
> in a class that manages some custom bindings and tgt is a reference to a
[quoted text clipped - 4 lines]
> Saying all of this, is it appropriate to set tgt to null here?  or should I
> do something else?

If you want the user control to actually "die" you'll need to remove it
from the canvas too. If you just want to not reference it any more from
tgt, then setting tgt to null is entirely appropriate.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

moondaddy - 28 Feb 2007 08:08 GMT
>> Thanks this looks like just what I need.  By the way, this code is
>> running
[quoted text clipped - 12 lines]
> from the canvas too. If you just want to not reference it any more from
> tgt, then setting tgt to null is entirely appropriate.
moondaddy - 28 Feb 2007 08:08 GMT
Great!  Thanks alot.

>> Thanks this looks like just what I need.  By the way, this code is
>> running
[quoted text clipped - 12 lines]
> from the canvas too. If you just want to not reference it any more from
> tgt, then setting tgt to null is entirely appropriate.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.