void OnProgressChanged(object o, ProgressChangedEventArgs args) { ProgressReport report = (ProgressReport)args.UserState; if (null == report) // Huh? return; _lastProgressReport = report; int progress = (int)(((float)report.Processed / (float)report.TotalFiles) * 90); pgbProgress.Value = progress + 10; string missingReport; if (report.FilesSkipped <= 0) missingReport = String.Empty; else missingReport = String.Format(" ({0} not found)", report.FilesSkipped); lblProgressText.Text = String.Format("{0}/{1}{2}", report.Processed, report.TotalFiles, missingReport); lblCurrentAction.Text = Path.GetFileName( report.CurrentFileName ); }
/// <summary> /// Background file copy delegate /// </summary> void DoWork(object o, DoWorkEventArgs args) { List<Pair<String,String> > tasks = (List<Pair<String,String> >)args.Argument; BackgroundWorker bw = o as BackgroundWorker; int total = tasks.Count; int nCopied = 0; int nSkipped = 0; int i = 0; // number of files processed foreach (Pair<string, string> pair in tasks) { i++; if (bw.CancellationPending) { args.Cancel = true; return; } ProgressReport report = new ProgressReport(nCopied, nSkipped, i, total, pair.Right); bw.ReportProgress(i / total, report); try { if (cbMoveFiles.Checked) // TODO: this is probably not thread safe { System.IO.File.Move(pair.Left, pair.Right); } else { System.IO.File.Copy(pair.Left, pair.Right, true); } ++nCopied; } catch (FileNotFoundException) { // Do nothing, just skip the file ++nSkipped; } catch (DirectoryNotFoundException) { ++nSkipped; } catch (IOException ioe) { // delete the partial output file if it exists File.Delete(pair.Right); // If it's an "out of space" exception, exit quietly if ((ioe.HResult & 0xffff) == ERROR_DISK_FULL) { args.Result = CopyResult.OUT_OF_SPACE; return; // ** quick exit ** Finished early. } throw; // otherwise, exit noisily } catch (Exception) { // delete the output file on the assumption that it's incomplete File.Delete(pair.Right); throw; } } args.Result = CopyResult.SUCCESS; // no exceptions or ANYTHING. }