public async static Task <byte[]> ComputeHash <T>(Stream stream) where T : HashAlgorithm, new() { // Init var alg = new T(); long total = 0; byte[] block = new byte[1024 * 1024]; // 1 MB at a time, just for smoother progress long len = stream.Length; if (len > 0) { string name = stream is FileStream ? $"Calculating SHA256 of '{(stream as FileStream).Name}'" : $"Calculatig SHA256 of {len} byte data"; ProgressJob job = new ProgressJob(name, len, 0); job.Start(); while (total < len) { // For each block: int howMany = await stream.ReadAsync(block, 0, block.Length).ConfigureAwait(false); total += howMany; if (total >= len) { alg.TransformFinalBlock(block, 0, howMany); job.UpdateProgress(howMany); break; } else { alg.TransformBlock(block, 0, howMany, null, 0); job.UpdateProgress(howMany); } } // Get the has code byte[] hash = alg.Hash; job.Finish(); alg.Dispose(); return(hash); } string text = "Tried to hash empty stream"; if (stream is FileStream) { text += $" (File: {(stream as FileStream).Name}"; } throw new Exception(text); }
private void JobFinished(ProgressJob job) { JobTracker j = jobs[job]; //jobs.Remove(job); Dispatcher?.InvokeOrExecute(delegate { DownloadsPanel.Children.Remove(j.Container); DownloadsPanel.Children.Insert(DownloadsPanel.Children.Count, j.Container); }); if (job is DownloadJob dl) { logger.Info($"Finished downloading file '{dl.FileName}'."); } else { logger.Info($"Finished task '{job.JobName}'."); } }
private void JobStarted(ProgressJob job) { JobTracker tracker = new JobTracker { Job = job }; jobs.Add(job, tracker); Dispatcher?.InvokeOrExecute(delegate { var p = new StackPanel(); tracker.Container = p; // New progress bar ProgressBar bar = new ProgressBar { Minimum = 0, Maximum = job.ExpectedSize, Height = 25, Background = Brushes.LightGray, }; // Bind the Progress value to the Value property bar.SetBinding(ProgressBar.ValueProperty, new Binding("ProgressCompleted") { Source = job, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); var colorConverter = new DownloadProgressColorConverter(); bar.SetBinding(ProgressBar.ForegroundProperty, new Binding("Status") { Source = job, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Converter = colorConverter, }); bar.MouseDoubleClick += (s, a) => job.Cancel(); TextBlock t = new TextBlock(); t.SetBinding(TextBlock.TextProperty, new Binding("ProgressSinceLastUpdate") { Source = job, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Converter = new DownloadProgressTextConverter(job) }); p.Children.Add(bar); p.Children.Add(t); p.UpdateLayout(); DownloadsPanel.Children.Insert(0, p); DownloadsPanel.ScrollOwner?.ScrollToTop(); DownloadsPanel.UpdateLayout(); }); if (job is FileWriteJob dj) { string kind = dj is DownloadJob ? "download" : "file write"; if (job.ProgressCompleted == 0) { logger.Info($"Starting {kind} of size {Miscellaneous.ToFileSize(job.ExpectedSize)}, File: '{dj.FileName}'."); } else { logger.Info($"Resuming {kind} at {Miscellaneous.ToFileSize(job.ProgressCompleted)}/{Miscellaneous.ToFileSize(job.ExpectedSize)}, File: '{dj.FileName}'."); } } else { logger.Info($"Starting job '{job.JobName}'"); } }
public DownloadProgressTextConverter(ProgressJob job) { this.job = job; }
private void JobProgress(ProgressJob download, int progressSinceLast) { //Download dl = downloads[download.FileName]; }