/// <summary> /// Start the queue handling. Will check the queue and then saves the images one by one. /// </summary> private void StartQueue() { frameCnt = 0; IsRecording = true; Task.Factory.StartNew(() => { //while (IsRecording) // Queue.Count > 0 is added to the condition here // to ensure that all ImageToSave added to the Queue will be saved as a file while (!IsAllBufferFramesSaved) { if (Queue.Count == 0) { continue; } ImageToSave imageToSave = null; if (Queue.TryTake(out imageToSave)) { string filePath = Path.Combine(imageToSave.FolderPath, frameCnt.ToString().PadLeft(8, '0') + ".png"); //Debug.WriteLine("1: Saving image from queue to {0}", filePath); try { imageToSave.VBitmap.Save(filePath); frameCnt++; //Debug.WriteLine("1: Queue is holding {0} images", Queue.Count); } catch (Exception ex) { Debug.WriteLine("1: Error reading and executing queue", ex); } finally { imageToSave.Dispose(); } } } }); }