/// <summary> /// Deregisters an existing handler from this event. /// </summary> /// <param name="registrator"></param> /// <param name="handler"></param> /// <returns></returns> public bool Deregister(object registrator, EventHandler <ArgumentsType> handler) { // Create the registration object to locate.. var registration = new EventHandlerRegistration <ArgumentsType> { Handler = handler, Registrator = registrator }; lock (this._registrationsLock) { // Attempt to locate the registration.. var registrationIndex = this._registrations.IndexOf(registration); if (registrationIndex == -1) { return(false); } // Create clone of current registrations to manipulate.. var registrations = new List <EventHandlerRegistration <ArgumentsType> >(this._registrations.Count); registrations.AddRange(this._registrations.Where((t, x) => x != registrationIndex)); // Update the registrations.. Interlocked.Exchange(ref this._registrations, registrations); } return(true); }
/// <summary> /// Registers a new handler to this event. /// </summary> /// <param name="registrator"></param> /// <param name="handler"></param> /// <param name="priority"></param> public void Register(object registrator, EventHandler <ArgumentsType> handler, int priority) { // Ensure the priority is valid.. if (registrator != null && priority == 0) { throw new InvalidOperationException("Cannot register a new event with a priority of 0."); } // Create the new registration object.. var registration = new EventHandlerRegistration <ArgumentsType> { Handler = handler, Priority = priority, Registrator = registrator }; lock (this._registrationsLock) { // Create clone of current registrations to manipulate.. var registrations = new List <EventHandlerRegistration <ArgumentsType> >(this._registrations.Count); registrations.AddRange(this._registrations); // Locate the proper priority position.. var insertIndex = registrations.Count; for (var x = 0; x < registrations.Count; x++) { if (registrations[x].Priority > priority) { insertIndex = x; break; } } // Insert the new registration at the found index.. registrations.Insert(insertIndex, registration); Interlocked.Exchange(ref this._registrations, registrations); } }