private async Task RunSelfTest() { SetBusy(true); m_abortFlag.Reset(); try { await Task.Run(() => MHash384.SelfTest(m_abortFlag)); String message = "Self-test completed successfully."; Edit_HashValue.Text = message; MessageBox.Show(message, "Self-test", MessageBoxButton.OK, MessageBoxImage.Information); } catch (OperationCanceledException err) { String message = String.IsNullOrEmpty(err.Message) ? err.GetType().FullName : err.Message; Edit_HashValue.Text = message; MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Warning); } catch (Exception err) { String message = String.IsNullOrEmpty(err.Message) ? err.GetType().FullName : err.Message; Edit_HashValue.Text = "Failure: " + message; } finally { SetBusy(false); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { ushort major, minor, patch; MHash384.GetVersion(out major, out minor, out patch); Title += String.Format(" v{0:D}.{1:D}.{2:D}", major, minor, patch); KeyDown += new KeyEventHandler(KeyDownHandler); }
private String ComputeHash(Dispatcher dispatcher, String fileName, ProgressHandler progressHandler) { MHash384 digest = new MHash384(); FileInfo fileInfo = new FileInfo(fileName); Stopwatch stopWatch = new Stopwatch(); m_abortFlag.Reset(); using (BufferedStream stream = new BufferedStream(fileInfo.OpenRead(), BUFF_SIZE)) { double progress = 0.0; ushort update = 0; Action updateAction = new Action(() => progressHandler(progress, stopWatch.ElapsedMilliseconds / 1000.0)); Task updateTask = null; stopWatch.Start(); for (;;) { if ((++update & 0x1FF) == 0) { if (m_abortFlag.WaitOne(TimeSpan.Zero)) { throw new OperationCanceledException("The operation was cancelled by the user!"); } AwaitTaskSynchronously(updateTask); progress = ((double)stream.Position) / ((double)fileInfo.Length); updateTask = dispatcher.BeginInvoke(DispatcherPriority.Background, updateAction).Task; } if (!digest.Update(stream, BUFF_SIZE)) { break; /*EOF*/ } } stopWatch.Stop(); AwaitTaskSynchronously(updateTask); dispatcher.Invoke(() => progressHandler(1.0, stopWatch.ElapsedMilliseconds / 1000.0)); return(digest.ToString()); } }