469,352 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to remove event handler

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.

mo*******@noemail.noemail
Feb 27 '07 #1
8 8529
moondaddy <mo*******@noemail.noemailwrote:
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.
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);
--
Jon Skeet - <sk***@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
Feb 27 '07 #2
Wouldn't that fail due to the SizeChangedEventHandler being a different
instance than was added via +=?

Feb 27 '07 #3
Keith Patrick <ri*******************@nospam.hotmail.comwrote:
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.

--
Jon Skeet - <sk***@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
Feb 27 '07 #4
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!

Feb 27 '07 #5
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?
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
moondaddy <mo*******@noemail.noemailwrote:
>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.

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);
--
Jon Skeet - <sk***@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

Feb 28 '07 #6
moondaddy <mo*******@noemail.noemailwrote:
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?
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.

--
Jon Skeet - <sk***@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
Feb 28 '07 #7
Great! Thanks alot.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
moondaddy <mo*******@noemail.noemailwrote:
>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?

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.

--
Jon Skeet - <sk***@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

Feb 28 '07 #8

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
moondaddy <mo*******@noemail.noemailwrote:
>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?

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.

--
Jon Skeet - <sk***@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

Feb 28 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chien Lau | last post: by
1 post views Thread by Daves | last post: by
3 posts views Thread by Franco, Gustavo | last post: by
3 posts views Thread by Peter Oliphant | last post: by
1 post views Thread by Richard W | last post: by
10 posts views Thread by Franky | last post: by
2 posts views Thread by =?Utf-8?B?R3JlZ29yeSBLaHJh?= | last post: by
4 posts views Thread by =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.