123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace FastReport.Utils
- {
- internal static class Throttle
- {
- private static readonly Dictionary<Action, Timer> timers = new Dictionary<Action, Timer>();
- 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
- }
- }
- }
|