private async Task UpdateImageAsync() { GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency; var sourceDir = new DirectoryInfo(SourcePath); IEnumerable <Task <ImageProcessorOutput> > resultTasks = sourceDir.EnumerateFiles(SourceFileMask) .OrderBy(f => { string lastPart = Path.GetFileNameWithoutExtension(f.Name).Split(' ').Last(); Int32.TryParse(lastPart, out int index); return(index); }) .Select(async file => { return(await Task.Run(async() => { DicomFile sourceFile = await DicomFile.OpenAsync(file.FullName); using (IImage iimage = new DicomImage(sourceFile.Dataset).RenderImage()) { Bitmap original = iimage.AsClonedBitmap(); ImageProcessor ip = new ImageProcessor(original); var result = ip.Process(); result.SourceFile = file; ImageOutput io = new ImageOutput(result.Images[1].Bitmap, result.Title); AllImages.Add(io); //Adding all images to List return result; } })); }).ToList(); foreach (var resultTask in resultTasks) { var result = await resultTask; var needRefresh = false; lock (_output) { int resultIndex = _output.Count; if (result.Positive && (resultIndex == 0 || result.SignificantlyDifferentFrom(_output[resultIndex - 1]))) { _output.Add(result); } UpdateStatLabels(); if (resultIndex == SelectedIndex) { needRefresh = true; } } if (needRefresh) { RefreshView(); } } }