public async Task <bool> QuickScan(IProgress <ScanAndRepairProgress> progress = null) { { if (IsScanRunning) { throw new Exception("Scan already running!"); } var retVal = true; IsScanRunning = true; try { _cts.Cancel(); _cts = new CancellationTokenSource(); IsCancellationRequested = false; var totalCount = FilesInfo.Count(); var currentIndex = 0; Parallel.ForEach(FilesInfo, (fileInfo, state) => { var index = Interlocked.Increment(ref currentIndex); _cts.Token.ThrowIfCancellationRequested(); progress?.Report(new ScanAndRepairProgress(totalCount, index, new ExLog(LogLevel.Info, $"{fileInfo.FileName}"))); RunFileQuickCheck(Path.Combine(FilesRootPath, fileInfo.FileName), fileInfo.Size); if (RunFileQuickCheck(Path.Combine(FilesRootPath, fileInfo.FileName), fileInfo.Size)) { return; } retVal = false; state.Break(); }); await Task.Delay(25, _cts.Token).ConfigureAwait(false); } finally { IsScanRunning = false; } return(retVal); } }
public async Task <bool> ScanAndRepair(IProgress <ScanAndRepairProgress> progress) { { if (IsScanRunning) { throw new Exception("Scan already running!"); } var retVal = false; IsScanRunning = true; try { _cts.Cancel(); _cts = new CancellationTokenSource(); IsCancellationRequested = false; CleanTmpFolder(); var t = Task.Run(async() => { try { var totalCount = FilesInfo.Count(); var currentIndex = 0; foreach (var fileInfo in FilesInfo.OrderByDescending(key => key.FileName.Contains("\\")) .ThenBy(key => key.FileName)) { currentIndex += 1; _cts.Token.ThrowIfCancellationRequested(); progress?.Report(new ScanAndRepairProgress(totalCount, currentIndex, new ExLog(LogLevel.Info, $"{fileInfo.FileName}"))); await Task.Delay(25, _cts.Token).ConfigureAwait(false); var fileProgress = new Progress <ScanAndRepairFileProgress>(); var ci = currentIndex; if (progress != null) { fileProgress.ProgressChanged += (o, ea) => { progress.Report(new ScanAndRepairProgress(totalCount, ci, ea)); } } ; retVal = await ScanAndRepairFile(fileInfo, FilesRootPath, fileProgress, _cts.Token); await Task.Delay(25, _cts.Token).ConfigureAwait(false); if (!retVal) { break; } } } catch (AggregateException) { retVal = false; throw; } }, _cts.Token); await t; } finally { IsScanRunning = false; } return(retVal); } }