How to remove event handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moondaddy

    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.SizeCh anged += new SizeChangedEven tHandler(OnTarg etSizeChanged);



    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 'OnTargetSizeCh anged' 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@noema il.noemail


  • Jon Skeet [C# MVP]

    #2
    Re: How to remove event handler

    moondaddy <moondaddy@noem ail.noemailwrot e:
    I'm working in WPF and c# and am adding an event handler to an object like
    this:
    >
    this.tgt.SizeCh anged += new SizeChangedEven tHandler(OnTarg etSizeChanged);
    >
    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 'OnTargetSizeCh anged' 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.SizeCh anged -= new SizeChangedEven tHandler
    (OnTargetSizeCh anged);


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

    Comment

    • Keith Patrick

      #3
      Re: How to remove event handler

      Wouldn't that fail due to the SizeChangedEven tHandler being a different
      instance than was added via +=?



      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: How to remove event handler

        Keith Patrick <richard_keith_ patrick@nospam. hotmail.comwrot e:
        Wouldn't that fail due to the SizeChangedEven tHandler being a different
        instance than was added via +=?
        No. They'd still compare as being equal, which is what's used.

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

        Comment

        • Keith Patrick

          #5
          Re: How to remove event handler

          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!



          Comment

          • moondaddy

            #6
            Re: How to remove event handler

            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]" <skeet@pobox.co mwrote in message
            news:MPG.204eaa b454353bec98d8e 3@msnews.micros oft.com...
            moondaddy <moondaddy@noem ail.noemailwrot e:
            >I'm working in WPF and c# and am adding an event handler to an object
            >like
            >this:
            >>
            >this.tgt.SizeC hanged += new SizeChangedEven tHandler(OnTarg etSizeChanged);
            >>
            >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 'OnTargetSizeCh anged' 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.SizeCh anged -= new SizeChangedEven tHandler
            (OnTargetSizeCh anged);
            >
            >
            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: How to remove event handler

              moondaddy <moondaddy@noem ail.noemailwrot e:
              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 - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • moondaddy

                #8
                Re: How to remove event handler

                Great! Thanks alot.


                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:MPG.204f40 06e7c92e1b98d8e 8@msnews.micros oft.com...
                moondaddy <moondaddy@noem ail.noemailwrot e:
                >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 - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • moondaddy

                  #9
                  Re: How to remove event handler


                  "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  news:MPG.204f40 06e7c92e1b98d8e 8@msnews.micros oft.com...
                  moondaddy <moondaddy@noem ail.noemailwrot e:
                  >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 - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  Working...