namespace HaloPixelToolBox.Core.Utilities; /// Keep thumbnail work tied to its track, including asynchronous retries. public sealed class AlbumColorCache { private readonly object _lock = new(); private string _trackKey = string.Empty; private int _generation; private bool _updating; private long _retryAt; private (byte R, byte G, byte B)? _color; public (byte R, byte G, byte B)? CurrentColor { get { lock (_lock) return _color; } } public void SetTrack(string trackKey) { lock (_lock) { if (_trackKey == trackKey) return; _trackKey = trackKey; _generation++; _color = null; _updating = false; _retryAt = 0; } } public bool TryBeginUpdate(out int generation) { lock (_lock) { generation = _generation; if (_color.HasValue || _updating || Environment.TickCount64 < _retryAt) return false; _updating = true; return true; } } public void CompleteUpdate(int generation, (byte R, byte G, byte B)? color) { lock (_lock) { if (generation != _generation) return; _color = color; _updating = false; _retryAt = color.HasValue ? 0 : Environment.TickCount64 + 1000; } } }