private string HashCheckThread(string filePath, HashType hashType, IProgress <float> progress, CancellationToken ct) { using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (HashAlgorithm hash = GetHashAlgorithm(hashType)) using (CryptoStream cs = new CryptoStream(stream, hash, CryptoStreamMode.Read)) { long bytesRead, totalRead = 0; byte[] buffer = new byte[8192]; Stopwatch timer = Stopwatch.StartNew(); while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0 && !ct.IsCancellationRequested) { totalRead += bytesRead; if (timer.ElapsedMilliseconds > 200) { float percentage = (float)totalRead / stream.Length * 100; progress.Report(percentage); timer.Reset(); timer.Start(); } } if (ct.IsCancellationRequested) { progress.Report(0); ct.ThrowIfCancellationRequested(); } else { progress.Report(100); string[] hex = TranslatorHelper.BytesToHexadecimal(hash.Hash); return(string.Concat(hex)); } } return(null); }
private void CheckThread(object sender, DoWorkEventArgs e) { using (FileStream stream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (HashAlgorithm hash = GetHashAlgorithm(HashType)) using (CryptoStream cs = new CryptoStream(stream, hash, CryptoStreamMode.Read)) { long bytesRead, totalRead = 0; byte[] buffer = new byte[8192]; Stopwatch timer = Stopwatch.StartNew(); while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0 && !bw.CancellationPending) { totalRead += bytesRead; if (timer.ElapsedMilliseconds > 200) { float progress = (float)totalRead / stream.Length * 100; bw.ReportProgress(0, progress); timer.Reset(); timer.Start(); } } if (bw.CancellationPending) { bw.ReportProgress(0, 0f); e.Cancel = true; } else { bw.ReportProgress(0, 100f); string[] hex = TranslatorHelper.BytesToHexadecimal(hash.Hash); e.Result = string.Concat(hex); } } }