/// <summary> /// Unhooks an <see cref="AsynchronousEventHandler{T}"/> from this <see cref="AsynchronousEvent{T}"/>. /// </summary> /// <param name="handler"> The <see cref="AsynchronousEventHandler{T}"/> to unhook. </param> public void Unhook(AsynchronousEventHandler <T> handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } lock (_lock) { Delegate -= handler; } }
/// <summary> /// Unhooks an <see cref="AsynchronousEventHandler{T}"/> from this <see cref="AsynchronousEvent{T}"/>. /// </summary> /// <param name="handler"> The <see cref="AsynchronousEventHandler{T}"/> to unhook. </param> public void Unhook(AsynchronousEventHandler <TEventArgs> handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } lock (this) { var currentHandlers = _handlers; var newHandlers = currentHandlers.Remove(handler); if (currentHandlers == newHandlers) { throw new ArgumentException($"The event handler {handler} was never hooked to this event."); } _handlers = newHandlers; } }
/// <summary> /// Hooks an <see cref="AsynchronousEventHandler{T}"/> up to this <see cref="AsynchronousEvent{T}"/>. /// </summary> /// <param name="handler"> The <see cref="AsynchronousEventHandler{T}"/> to hook up. </param> public void Hook(AsynchronousEventHandler <TEventArgs> handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } lock (this) { var currentHandlers = _handlers; var newHandlers = currentHandlers.Add(handler); if (currentHandlers == newHandlers) { throw new ArgumentException($"The event handler {handler} is already hooked to this event. Did you hook it up twice by mistake?"); } _handlers = newHandlers; } }