How can I clear event subscriptions in C#?
Asked Answered
T

10

152

Take the following C# class:

c1 {
 event EventHandler someEvent;
}

If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? Also consider that subscriptions to this event could be/are lambdas/anonymous delegates.

Currently my solution is to add a ResetSubscriptions() method to c1 that sets someEvent to null. I don't know if this has any unseen consequences.

Throughway answered 30/9, 2008 at 15:32 Comment(1)
I described a working answer using Reflection here: #92278Jacinthe
I
194

From within the class, you can set the (hidden) variable to null. A null reference is the canonical way of representing an empty invocation list, effectively.

From outside the class, you can't do this - events basically expose "subscribe" and "unsubscribe" and that's it.

It's worth being aware of what field-like events are actually doing - they're creating a variable and an event at the same time. Within the class, you end up referencing the variable. From outside, you reference the event.

See my article on events and delegates for more information.

Iou answered 30/9, 2008 at 16:5 Comment(5)
If you're stubborn, you can force it clear via reflection. See #92278 .Lutist
@Brian: It depends on the implementation. If it's just a field-like event or an EventHandlerList, you may be able to. You'd have to recognise those two cases though - and there could be any number of other implementations.Iou
@Joshua: No, it will set the variable to have a value of null. I agree that the variable won't be called hidden.Iou
@JonSkeet That's what I (thought) I said. The way it was written confused me for 5 minutes.Miun
@JoshuaLamusga: Well you said it would clear an invocation list, which sounds like modifying an existing object.Iou
T
39

Add a method to c1 that will set 'someEvent' to null.

public class c1
{
    event EventHandler someEvent;
    public ResetSubscriptions() => someEvent = null;    
}
Throughway answered 30/9, 2008 at 15:33 Comment(1)
That is the behavior I am seeing. As I said in my question, I don't know if I'm overlooking something.Throughway
R
11
class c1
{
    event EventHandler someEvent;
    ResetSubscriptions() => someEvent = delegate { };
}

It is better to use delegate { } than null to avoid the null ref exception.

Ricardoricca answered 30/4, 2014 at 16:33 Comment(3)
Why? Could you please expand on this answer?Lusty
@S.Buda Because if it's null then you'll get a null ref. It's like using a List.Clear() vs myList = null.Modred
Given that you can do someEvent?.Invoke(...) in modern C# I'm not sure if this is important, or if the slight lack of clarity of using a noop delegate is worth it.Illona
S
7

The best practice to clear all subscribers is to set the someEvent to null by adding another public method if you want to expose this functionality to outside. This has no unseen consequences. The precondition is to remember to declare SomeEvent with the keyword 'event'.

Please see the book - C# 4.0 in the nutshell, page 125.

Some one here proposed to use Delegate.RemoveAll method. If you use it, the sample code could follow the below form. But it is really stupid. Why not just SomeEvent=null inside the ClearSubscribers() function?

public void ClearSubscribers ()
{
   SomeEvent = (EventHandler) Delegate.RemoveAll(SomeEvent, SomeEvent);
   // Then you will find SomeEvent is set to null.
}
Sublimity answered 16/10, 2012 at 12:0 Comment(1)
Delegate.RemoveAll valid for MulticastDelegate: public delegate string TableNameMapperDelegate(Type type);public static TableNameMapperDelegate TableNameMapper; ?Summation
L
6

Setting the event to null inside the class works. When you dispose a class you should always set the event to null, the GC has problems with events and may not clean up the disposed class if it has dangling events.

Lenticular answered 1/10, 2008 at 11:17 Comment(0)
H
5

You can achieve this by using the Delegate.Remove or Delegate.RemoveAll methods.

Hapte answered 30/9, 2008 at 15:36 Comment(1)
I don't believe this will work with lambda expressions or anonymous delegates.Throughway
M
3

Conceptual extended boring comment.

I rather use the word "event handler" instead of "event" or "delegate". And used the word "event" for other stuff. In some programming languages (VB.NET, Object Pascal, Objective-C), "event" is called a "message" or "signal", and even have a "message" keyword, and specific sugar syntax.

const
  WM_Paint = 998;  // <-- "question" can be done by several talkers
  WM_Clear = 546;

type
  MyWindowClass = class(Window)
    procedure NotEventHandlerMethod_1;
    procedure NotEventHandlerMethod_17;

    procedure DoPaintEventHandler; message WM_Paint; // <-- "answer" by this listener
    procedure DoClearEventHandler; message WM_Clear;
  end;

And, in order to respond to that "message", a "event handler" respond, whether is a single delegate or multiple delegates.

Summary: "Event" is the "question", "event handler (s)" are the answer (s).

Middleaged answered 24/3, 2011 at 18:39 Comment(0)
F
2

Remove all events, assume the event is an "Action" type:

Delegate[] dary = TermCheckScore.GetInvocationList();

if ( dary != null )
{
    foreach ( Delegate del in dary )
    {
        TermCheckScore -= ( Action ) del;
    }
}
Frontispiece answered 20/12, 2013 at 20:0 Comment(1)
If you're inside of the type that declared the event you don't need to do this, you can just set it to null, if you're outside of the type then you can't get the invocation list of the delegate. Also, your code throws an exception if the event is null, when calling GetInvocationList.Rishi
T
0

This is my solution:

public class Foo : IDisposable
{
    private event EventHandler _statusChanged;
    public event EventHandler StatusChanged
    {
        add
        {
            _statusChanged += value;
        }
        remove
        {
            _statusChanged -= value;
        }
    }

    public void Dispose()
    {
        _statusChanged = null;
    }
}

You need to call Dispose() or use using(new Foo()){/*...*/} pattern to unsubscribe all members of invocation list.

Triangle answered 5/3, 2018 at 7:14 Comment(0)
D
-1

Instead of adding and removing callbacks manually and having a bunch of delegate types declared everywhere:

// The hard way
public delegate void ObjectCallback(ObjectType broadcaster);

public class Object
{
    public event ObjectCallback m_ObjectCallback;
    
    void SetupListener()
    {
        ObjectCallback callback = null;
        callback = (ObjectType broadcaster) =>
        {
            // one time logic here
            broadcaster.m_ObjectCallback -= callback;
        };
        m_ObjectCallback += callback;

    }
    
    void BroadcastEvent()
    {
        m_ObjectCallback?.Invoke(this);
    }
}

You could try this generic approach:

public class Object
{
    public Broadcast<Object> m_EventToBroadcast = new Broadcast<Object>();

    void SetupListener()
    {
        m_EventToBroadcast.SubscribeOnce((ObjectType broadcaster) => {
            // one time logic here
        });
    }

    ~Object()
    {
        m_EventToBroadcast.Dispose();
        m_EventToBroadcast = null;
    }

    void BroadcastEvent()
    {
        m_EventToBroadcast.Broadcast(this);
    }
}


public delegate void ObjectDelegate<T>(T broadcaster);
public class Broadcast<T> : IDisposable
{
    private event ObjectDelegate<T> m_Event;
    private List<ObjectDelegate<T>> m_SingleSubscribers = new List<ObjectDelegate<T>>();

    ~Broadcast()
    {
        Dispose();
    }

    public void Dispose()
    {
        Clear();
        System.GC.SuppressFinalize(this);
    }

    public void Clear()
    {
        m_SingleSubscribers.Clear();
        m_Event = delegate { };
    }

    // add a one shot to this delegate that is removed after first broadcast
    public void SubscribeOnce(ObjectDelegate<T> del)
    {
        m_Event += del;
        m_SingleSubscribers.Add(del);
    }

    // add a recurring delegate that gets called each time
    public void Subscribe(ObjectDelegate<T> del)
    {
        m_Event += del;
    }

    public void Unsubscribe(ObjectDelegate<T> del)
    {
        m_Event -= del;
    }

    public void Broadcast(T broadcaster)
    {
        m_Event?.Invoke(broadcaster);
        for (int i = 0; i < m_SingleSubscribers.Count; ++i)
        {
            Unsubscribe(m_SingleSubscribers[i]);
        }
        m_SingleSubscribers.Clear();
    }
}
Dionysus answered 29/1, 2020 at 15:37 Comment(2)
Can you please format your question and remove all the white space on the left? When you copy and paste from an IDE this can happenModred
Just got rid of that white space, my badDionysus

© 2022 - 2024 — McMap. All rights reserved.