using System; using System.Threading; namespace FluidSim { /// /// Tracks the duty cycle of a worker thread using an exponential moving average. /// Thread‑safe: one writer (the sim thread), any reader (UI thread). /// public class ThreadLoadTracker { private double _loadPercent; // 0 .. 100, accessed with Volatile.Read/Write private const double Alpha = 0.1; // smoothing factor (higher = faster response) /// /// Update the load percentage with a new observation. /// /// Time spent on real work in the last cycle. /// Total time of the last cycle (work + idle). If zero, ignored. public void Record(double busyMs, double totalMs) { if (totalMs <= 0) return; double instantLoad = busyMs / totalMs * 100.0; // Exponential moving average double old = Volatile.Read(ref _loadPercent); double newLoad = old + Alpha * (instantLoad - old); Volatile.Write(ref _loadPercent, newLoad); } /// Current smoothed load percentage (0‑100). public double LoadPercent => Volatile.Read(ref _loadPercent); } }