static void BackgroundDownloader() { while (!DownloadQueue.IsCompleted) { if (!DownloadQueue.TryTake(out var def, System.Threading.Timeout.InfiniteTimeSpan)) { continue; } var bytes = downloadBytes(def); lock (cacheLocker) cache[def] = bytes; PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes }); } }
private static void timerDownload() { // must live outside try-catch, else 'finally' can reset another thread's lock if (isProcessing) { return; } try { isProcessing = true; var def = cache .Where(kvp => kvp.Value is null) .Select(kvp => kvp.Key) // 80x80 should be 1st since it's enum value == 0 .OrderBy(d => d.PictureId) .FirstOrDefault(); // no more null entries. all requsted images are cached if (string.IsNullOrWhiteSpace(def.PictureId)) { return; } var bytes = downloadBytes(def); saveFile(def, bytes); cache[def] = bytes; PictureCached?.Invoke(nameof(PictureStorage), def.PictureId); } finally { isProcessing = false; } }