/// <summary> /// Adds a collection of events into listeners. /// </summary> /// <param name="listeners"></param> /// <exception cref="InvalidOperationException"></exception> public void AddEventListeners(KeyValuePair <TimeSpan, Action>[] listeners) { try { switch (GetFlag) { default: break; case AutomatedRestarterFlag.Running | AutomatedRestarterFlag.Stopped: throw new InvalidOperationException($"You cannot perform new listener addition when in {GetFlag} mode."); } foreach (var listener in listeners) { var entry = new AutomatedRestarterListener { Timeout = listener.Key, Handler = listener.Value }; if (entry.IsInvalid) { throw new InvalidOperationException("The listener is invalid."); } this.listeners.Add(entry); } } catch (InvalidOperationException e) { onError.Invoke(this, e); } }
/// <summary> /// Adds a new event into listeners. /// </summary> /// <param name="timeout"></param> /// <param name="action"></param> /// <exception cref="InvalidOperationException"></exception> public void AddEventListener(TimeSpan timeout, Action action) { switch (GetFlag) { default: break; case AutomatedRestarterFlag.Running | AutomatedRestarterFlag.Stopped: throw new InvalidOperationException($"You cannot perform new listener addition when in {GetFlag} mode."); } var entry = new AutomatedRestarterListener { Timeout = timeout, Handler = action }; if (entry.IsInvalid) { throw new InvalidOperationException("The listener is invalid."); } listeners.Add(entry); }