IProgressEstimator - PCAssistSoftware/RoboSharp GitHub Wiki

Purpose:

This interface was designed to be used by a user interface to report live results of a robocopy as the process runs. The IProgressEstimator is created as the copy operation begins, but after validation occurs (such as ensuring the paths exist). See 'Typical Usage' below for how to work into your application.

This interface is for reporting number of files and directories, not file copy progress, which is already reported via IRoboCommand.OnCopyProgressChanged. See IRoboCommand

Implemented By:

Properties:

Property Name Property Type Description
DirectoriesStatistic IStatistic Estimate of current number of directories processed while the job is still running.
FilesStatistic IStatistic Estimate of current number of files processed while the job is still running.
BytesStatistic IStatistic Estimate of current number of bytes processed while the job is still running.

Events:

Name Event Object Delegate Description
ValuesUpdated IProgressEstimatorUpdateEventArgs UIUpdateEventHandler This event is fired when the values of the progress estimator are updated. Default implementations will only raise the event every 150ms in order to avoid overloading the UI.

Methods:

Typical Usage

Example App : https://github.com/tjscience/RoboSharp/blob/dev/RoboSharp.BackupApp/ViewModels/CommandProgressViewModel.cs

Overview:

  • Subscribe to IRoboCommand.OnProgressEstimatorCreated
  • Start the IRoboCommand.
  • Once OnProgressEstimatorCreated fires, you can subscribe to ValuesUpdated to update the UI.
//example using a viewmodel

public IProgressEstimator CurrentOperationStatistics {get; private set;}

private async Task RunCommand(IRoboCommand cmd){
    cmd.OnProgressEstimatorCreated += BindToProgressEstimator;
    cmd.OnFileProcessed += FileProcessed; // implement a handler to record the current file and display the name to user
    cmd.OnCopyProgressChanged+= UpdateCopyProgress; // updates when the file progress updates
    await cmd.Start();
    // unsubscribe
}

private void BindToProgressEstimator(object sender, EventArgObjects.ProgressEstimatorCreatedEventArgs e)
{
    CurrentOperationStatistics= e.ResultsEstimate;
    CurrentOperationStatistics.ValuesUpdated += EstimatorValuesUpdated;
}

// Raise PropertyChanged for the progress estimator. 
// The UI might be bound to IProgressEstimator.FilesStatistic.Total to show total number of files processed
private void EstimatorValuesUpdated(object sender, EventArgs e) => OnPropertyChanged(nameof(CurrentOperationStatistics));

Usage for progress estimator that shows current number of files out of X completed:

public double TotalProgress {get; private set; }

private RoboCopyResults listResults;

private async Task RunCommand(IRoboCommand cmd)
{
    // await the list-only results, which is significantly faster than copy operation
    // the only logs that matter for this is the summary, as that is parsed into the results object
    listResults = await cmd.StartAsync_ListOnly(); 

    // bind to the command for UI purposes
    cmd.OnProgressEstimatorCreated += BindToProgressEstimator;
    cmd.OnFileProcessed += FileProcessed; // implement a handler to record the current file and display the name to user
    cmd.OnCopyProgressChanged+= UpdateCopyProgress; // updates when the file progress updates

    // run the command - StartAsync_ListOnly should set it to list only, complete, then turn listOnly OFF. 
    await cmd.Start();
    // unsubscribe
}

private void BindToProgressEstimator(object sender, EventArgObjects.ProgressEstimatorCreatedEventArgs e)
{
    CurrentOperationStatistics= e.ResultsEstimate;
    CurrentOperationStatistics.ValuesUpdated += EstimatorValuesUpdated;
}

// Report the number of files copied  / total files to copy
private void EstimatorValuesUpdated(object sender, IProgressEstimatorUpdateEventArgs e)
{
    TotalProgress = (double)(e.FilesStatistic.Total / listResults.FilesStatistic.Total)*100;
}
⚠️ **GitHub.com Fallback** ⚠️