private void OnProcessFile(object filename) { if (ProcessFile != null) { ProcessFile.Invoke(filename); } }
internal static double RunProcessFile(ProcessFile processFile, string filePath, IKeyValueCollection keyValueCollection, IUIntCollection uIntCollection) { double processTime = 0; try { var time = processFile?.Invoke(filePath, keyValueCollection, uIntCollection); processTime = time == null ? 0 : time.Value; } catch (OutOfMemoryException) { Console.WriteLine("Not enough memory. Couldn't perform this test."); processTime = 0; } catch (Exception) { Console.WriteLine("EXCEPTION. Couldn't perform this test."); processTime = 0; } GC.Collect(); Thread.Sleep(1000); return(processTime); }
public async Task <List <DuplicatesResult> > Find(int maxParallelism = 4) { var ts = new CancellationTokenSource(); cancellationTokens.Add(ts); CancellationToken ct = ts.Token; try { return(await Task.Run(() => { PrepareCompareValues?.Invoke(this, false); OnPrepareCompareValue(ct, maxParallelism); PrepareCompareValues?.Invoke(this, true); var result = new List <DuplicatesResult>(); ParallelOptions po = new ParallelOptions { CancellationToken = ct, MaxDegreeOfParallelism = maxParallelism }; try { int itemCounter = 0; decimal progressValue = 0; List <string> cacheIndexValues = new List <string>(); if (CacheDuplicateResult != null) { result = CacheDuplicateResult.DuplicatesResults; cacheIndexValues = CacheDuplicateResult.IndexCollection; } Parallel.For(0, Files.Count, po, (int index) => { string file = Files[index]; System.Diagnostics.Debug.Print($"INDEX => {index}"); if (!cacheIndexValues.Contains(file.ToUpper())) { if (itemCounter == 0) { ProcessFileProgress?.Invoke(this, 0); ProcessFileProgressWithItems?.Invoke(this, new ProcessFileProgressItem(0, result, index, itemCounter, cacheIndexValues)); } cacheIndexValues.Add(file.ToUpper()); ProcessFile?.Invoke(this, file); var dup = OnCompareDuplicates(file, index); if (dup != null) { result.Add(dup); } var progressValue2 = Math.Round(((decimal)itemCounter / (decimal)Files.Count * 100), 2); if (progressValue2 > progressValue) { progressValue = progressValue2; } ProcessFileProgress?.Invoke(this, progressValue); ProcessFileProgressWithItems?.Invoke(this, new ProcessFileProgressItem(progressValue, result, index, itemCounter, cacheIndexValues)); } else { System.Diagnostics.Debug.Print($"Already collected File:{file}"); } itemCounter++; }); ProcessFileProgress?.Invoke(this, 100); ProcessFileProgressWithItems?.Invoke(this, new ProcessFileProgressItem(100, result, -1, itemCounter, cacheIndexValues)); } catch (Exception ex) { if (ex is AggregateException || ex is ObjectDisposedException || ex is OperationCanceledException) { Aborted?.Invoke(this, EventArgs.Empty); return null; } throw; } cancellationTokens.Remove(ts); return result; }, ct)); } catch (Exception ex) { if (ex is AggregateException || ex is ObjectDisposedException || ex is OperationCanceledException) { Aborted?.Invoke(this, EventArgs.Empty); return(null); } throw; } }