using System; using System.Collections.Generic; using System.Windows.Forms; namespace FastReport.Utils { internal static class Throttle { private static readonly Dictionary timers = new Dictionary(); private static int suppressCount = 0; public static void Execute(Action action, int interval = 50) { #if (WPF || AVALONIA) // in WinForms, this may lead to exception (case: close designer and press "Yes" to save file => NRE) if (timers.ContainsKey(action)) { var timer = timers[action]; timer.Stop(); timer.Start(); suppressCount++; } else { var timer = new Timer() { Interval = interval }; timer.Tick += (s, e) => { timer.Dispose(); timers.Remove(action); if (action.Target is Control control && control.IsDisposed) return; action(); }; timers.Add(action, timer); timer.Start(); } #else action(); #endif } } }