/// <summary> /// Removes a control from the update list. /// If no managed controls remain in the list, then the update timer is stopped. /// </summary> /// <param name="control"> /// The <see cref="ICadencedControl"/> to be removed from the update list. /// </param> /// <remarks> /// If the control is null, or is not in the update list, no action is taken. /// If the update list is empty after the control is removed, then the cadence timer is stopped. /// </remarks> public void Remove(ICadencedControl control) { if (control == null) { return; } try { // Putting all the code in the finally block makes it a CONSTRAINED REGION that executes atomically // This prevents a timer event from happening in the middle of an add/remove operation. } finally { if (UpdateList.Contains(control)) { UpdateList.Remove(control); // Make sure this object doesn't receive any more timer updates. } // Stop the update timer if there are no items left in the update list. if (UpdateList.Count == 0) { CadenceTimer.Stop(); CadenceTimer.Elapsed -= TmrCadenceTick; // For symmetry, unregister the handler. } } }
/// <summary> /// Adds the specified <see cref="ICadencedControl" /> to the list of managed controls. If this is the first /// <paramref name="control" /> being added, then the update timer is configured and started. /// </summary> /// <remarks> /// Each <paramref name="control" /> can only appear in the list once (duplicate adds will be silently /// ignored). /// </remarks> /// <param name="control">The control to be managed.</param> public void Add(ICadencedControl control) { var hashCode = control.GetHashCode(); Trace.WriteLine($"Adding hash {hashCode}"); if (ControlList.ContainsKey(hashCode)) { Trace.TraceWarning($"Ignoring duplicate hash code {hashCode}."); } else { ControlList.Add(hashCode, new WeakReference <ICadencedControl>(control)); } if (updateTask == null) { StartUpdates(); } }
/// <summary> /// Removes a <paramref name="control" /> from the /// <see cref="TA.WinFormsControls.CadencedControlUpdater.ControlList" /> . If no managed controls remain in /// the list, then the update timer is stopped. /// </summary> public void Remove(ICadencedControl control) { try { var hash = control.GetHashCode(); Trace.TraceInformation($"Removing hash {hash}"); if (ControlList.ContainsKey(hash)) { ControlList.Remove(hash); } } catch (Exception e) { Trace.TraceError($"{e.GetType().Name} removing control: {e.Message}"); } finally { if (!ControlList?.Any() ?? false) { StopUpdates(); } } }
/// <summary> /// Adds the specified <see cref = "ICadencedControl" /> to the list of managed controls. /// If this is the first control being added, then the update timer is configured and started. /// </summary> /// <param name = "control">The control to be managed.</param> /// <remarks> /// Each control can only appear in the list once (duplicate adds will be silently ignored). /// </remarks> public void Add(ICadencedControl control) { try { // Putting all the code in the finally block makes it a CONSTRAINED REGION that executes atomically // This prevents a timer event from happening in the middle of an add/remove operation. } finally { if (!UpdateList.Contains(control)) { UpdateList.Add(control); // If this is the first instance, then create and start the timer. if (UpdateList.Count == 1) { CadenceTimer.Interval = 125; CadenceTimer.Elapsed += TmrCadenceTick; CadenceTimer.Start(); // Let rip. } } } }