Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Clearing event handlers

Clearing event handlers

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vineas
    wrote on last edited by
    #1

    Is there some way anyone knows to clear out the handlers for an event? An object I've created needs to clear all handlers to it at a point in the code where it is unknown what is attached to it. You can't just set the event to null, the compiler complains about that. I've also tried the following to no effect:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } } ...
    I thought it showed promise (the compiler didn't complain, and it did run fine) - unfortunately it simply didn't do anything. I've also tried some variations to the above, using the normal way of decoupling from an event, but by trying some of the values of the delegate to get the method:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= new MyEventHandler(meh.Method); } } ...
    - but the Delegate.Method property (as well as it's properties) don't seem to contain the right value wanted (the compiler makes noise about the fact that a property is being passed in to the handler instead of a method). I think I'm on the right track, but seem to be missing some key element to make it work. Anyone have any ideas? ----- In the land of the blind, the one eyed man is king.

    E 1 Reply Last reply
    0
    • V vineas

      Is there some way anyone knows to clear out the handlers for an event? An object I've created needs to clear all handlers to it at a point in the code where it is unknown what is attached to it. You can't just set the event to null, the compiler complains about that. I've also tried the following to no effect:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } } ...
      I thought it showed promise (the compiler didn't complain, and it did run fine) - unfortunately it simply didn't do anything. I've also tried some variations to the above, using the normal way of decoupling from an event, but by trying some of the values of the delegate to get the method:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= new MyEventHandler(meh.Method); } } ...
      - but the Delegate.Method property (as well as it's properties) don't seem to contain the right value wanted (the compiler makes noise about the fact that a property is being passed in to the handler instead of a method). I think I'm on the right track, but seem to be missing some key element to make it work. Anyone have any ideas? ----- In the land of the blind, the one eyed man is king.

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      Vineas wrote:

      ...

      if (MyEvent != null)
      {
      Delegate[] delegateList = MyEvent.GetInvocationList();
      foreach (MyEventHandler meh in delegateList)
      {
      MyEvent -= new MyEventHandler(meh.Method);
      }
      }

      Try using:

      if (MyEvent != null)
      {
      Delegate[] delegateList = MyEvent.GetInvocationList();
      foreach (MyEventHandler meh in delegateList)
      {
      MyEvent -= meh;
      }
      ]

      Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Ed

      V 1 Reply Last reply
      0
      • E Ed Poore

        Vineas wrote:

        ...

        if (MyEvent != null)
        {
        Delegate[] delegateList = MyEvent.GetInvocationList();
        foreach (MyEventHandler meh in delegateList)
        {
        MyEvent -= new MyEventHandler(meh.Method);
        }
        }

        Try using:

        if (MyEvent != null)
        {
        Delegate[] delegateList = MyEvent.GetInvocationList();
        foreach (MyEventHandler meh in delegateList)
        {
        MyEvent -= meh;
        }
        ]

        Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Ed

        V Offline
        V Offline
        vineas
        wrote on last edited by
        #3

        Ed.Poore wrote:

        Try using: if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } }
        Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Ed

        uh - did you read my initial post?!?!?!? That is exactly the first code snippet I listed (which does not work BTW - which then prompted me to continue to the other snippet, which also didn't work). I still haven't found a solution to this. Some of the things I've found when searching around seem to indicate that others have tried and failed - so in the interest of brevity (and better code), for my own purposes, I re-did the section in question so am no longer facing this issue.* I am still interested in a solution though - there have been other times I've wanted to do this, but haven't had the time to go much further - and from my searches I know others have wanted a solution as well. * in case anyone was wondering why this was needed to begin with - some code I wrote a while ago was doing a deep copy on some different objects by doing a MemberwiseClone(), then selectively doing deep copies on reference types in that object. This worked OK until I recently found that during the MemberwiseClone() call, an event from a base object was bringing all it's handlers with it when copied. D'oh! I wanted a way to clear out the event after copying - but it was better to rewrite the copy code to work correctly than to continue with a bad idea implemented because of a time crunch. ----- In the land of the blind, the one eyed man is king.

        E 1 Reply Last reply
        0
        • V vineas

          Ed.Poore wrote:

          Try using: if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } }
          Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Ed

          uh - did you read my initial post?!?!?!? That is exactly the first code snippet I listed (which does not work BTW - which then prompted me to continue to the other snippet, which also didn't work). I still haven't found a solution to this. Some of the things I've found when searching around seem to indicate that others have tried and failed - so in the interest of brevity (and better code), for my own purposes, I re-did the section in question so am no longer facing this issue.* I am still interested in a solution though - there have been other times I've wanted to do this, but haven't had the time to go much further - and from my searches I know others have wanted a solution as well. * in case anyone was wondering why this was needed to begin with - some code I wrote a while ago was doing a deep copy on some different objects by doing a MemberwiseClone(), then selectively doing deep copies on reference types in that object. This worked OK until I recently found that during the MemberwiseClone() call, an event from a base object was bringing all it's handlers with it when copied. D'oh! I wanted a way to clear out the event after copying - but it was better to rewrite the copy code to work correctly than to continue with a bad idea implemented because of a time crunch. ----- In the land of the blind, the one eyed man is king.

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          Sorry I was a bit quick in reading your post :-O I've had a brief look into this (which I neglected to do earlier and it seems that there is no easy or at least obvious way around this). I'll take a look later because you have me intrigued now. :) Ed

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups