private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { hashWriter = new HashWriter(this.OutputFile); try { hashWriter.OpenFileForWrite(); } catch (Exception) { OnProgressChanged?.Invoke(this, "Error writing output file: " + OutputFile); return; } bool finished = HashFiles(); if (backgroundWorker.CancellationPending) { hashWriter.CloseFile(); e.Cancel = true; return; } }