void TryUpdateImages() { ThreadSafety.TryLock(LatestWinnerImageLock, () => { bool updated; do { updated = false; foreach (var key in BitmapQueue.Keys.ToArray()) { var bq = BitmapQueue[key]; string lastRendered = null; while (bq.TryDequeue(out var g)) { lastRendered = g; } if (lastRendered == null) { continue; } updated = true; var latestFileName = $"LatestWinner.{key}.jpg"; try { File.Copy(lastRendered, Path.Combine(Environment.CurrentDirectory, latestFileName), true); } catch (IOException) { Debug.WriteLine($"Could not update {latestFileName}."); } }
public void Add(T value) { _values.Enqueue(value); while (_values.Count >= BatchSize && ThreadSafety.TryLock(_values, () => { // Use 'if' instead of 'while' to ensure a single 'Add' operation doesn't get trapped in the job of batching. if (_values.Count >= BatchSize) { _batches.Enqueue(_values.AsDequeueingEnumerable().Take(BatchSize).ToArray()); } })) { } }