static ImageHelper() { Task.Factory.StartNew(() => { var sec = TimeSpan.FromSeconds(30); while (true) { Thread.Sleep(sec); var now = DateTime.Now; var sb = Bitmaps.Where(k => now - k.Value.Item1 > sec).ToList(); for (var i = 0; i < sb.Count; i++) { var keyValuePair = sb[i]; Bitmaps.TryRemove(keyValuePair.Key, out _); } Logger.LogDebug($"cleared {sb.Count} cached images"); } }, TaskCreationOptions.LongRunning); }