public override void DoThread() { archiveCTS = new CancellationTokenSource(); var archiveCT = archiveCTS.Token; var ProgressLayout = new Windows.ProgressWindow("Archive") { CTS = archiveCTS }; var progress = new Progress <double>(ProgressLayout.ReportProgress); var realProgres = progress as IProgress <double>; ProgressLayout.Show(); Task.Run(async() => { var totalCount = filesQueue.Count(); tempCount = 0; var tasks = new Task[filesQueue.Length]; await Task.Run(() => { for (int i = 0; i < filesQueue.Length; i++) { var queue = filesQueue[i]; var queueLength = queue.Count; tasks[i] = Task.Run(() => { try { for (int j = 0; j < queueLength; j++) { archiveCT.ThrowIfCancellationRequested(); ArchiveFileInEntry(queue.Dequeue()); Interlocked.Increment(ref tempCount); lock (_archiveLock) { realProgres.Report(tempCount * 100 / (double)totalCount); } } } catch (OperationCanceledException) { MessageBox.Show("Archive canceled."); GC.Collect(2); GC.WaitForPendingFinalizers(); } }, archiveCT); } tasks.Wait(); }); ProgressLayout.Close(); Dispose(); GC.Collect(2); GC.WaitForPendingFinalizers(); }); }
public ThreadProcess Search(Queue <ISearchble>[] filesQueue, ActionWithThread <ISearchble> searchAndSaveIn) { return(async(process) => { seachCTS = new CancellationTokenSource(); var seachTC = seachCTS.Token; var totalCount = filesQueue.Count(); var ProgressLayout = new Windows.ProgressWindow("Search") { CTS = seachCTS }; var progress = new Progress <double>(ProgressLayout.ReportProgress); var realProgres = progress as IProgress <double>; ProgressLayout.Show(); await System.Threading.Tasks.Task.Run(() => { var tasks = new Task[filesQueue.Length]; var tempCount = 0; for (int i = 0; i < filesQueue.Length; i++) { var queue = filesQueue[i]; tasks[i] = Task.Run(() => { try { for (int j = 0; j < queue.Count; j++) { seachTC.ThrowIfCancellationRequested(); searchAndSaveIn(queue.Dequeue()); //Interlocked.Increment(ref tempCount); lock (_searchLock) realProgres.Report(++tempCount * 100 / (double)totalCount); } } catch (OperationCanceledException) { GC.Collect(2); GC.WaitForPendingFinalizers(); } }, seachTC); } tasks.Wait(); }); ProgressLayout.Close(); process(); }); }