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(); }); }
private async void StartDownloadAsync_Click(object sender, RoutedEventArgs e) { if (Link.Text == default(string) || Path.Text == default(string)) { MessageBox.Show("Please pick link, type and path."); return; } var link = Link.Text; var path = Path.Text; var fileRequest = WebRequest.Create(link); var fileResponse = fileRequest.GetResponse(); var BufferSize = 16384; var byteBuffer = new byte[BufferSize]; _wcCTS = new CancellationTokenSource(); var wcCT = _wcCTS.Token; var ProgressLayout = new Windows.ProgressWindow("Download") { CTS = _wcCTS }; var progress = new Progress <double>(ProgressLayout.ReportProgress); var realProgres = progress as IProgress <double>; ProgressLayout.Show(); await Task.Run(() => { using (var fromStream = fileResponse.GetResponseStream()) { var totalCount = fileResponse.ContentLength; tempCount = 0; using (var inStream = System.IO.File.Open(System.IO.Path.Combine(path, System.IO.Path.GetFileName(link)), FileMode.Create, FileAccess.Write, FileShare.Read)) { try { var bytesRead = 0; do { wcCT.ThrowIfCancellationRequested(); bytesRead = fromStream.Read(byteBuffer, 0, BufferSize); inStream.Write(byteBuffer, 0, bytesRead); tempCount += bytesRead; realProgres.Report(tempCount * 100 / (double)totalCount); } while (bytesRead > 0); } catch (OperationCanceledException) { MessageBox.Show("Download canceled."); } } } }, wcCT); }
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(); }); }