1

I'm trying to report progress to my status bar when creating a zip file.

For reporting the progress of the creation i have used the following guide:
ZipFile Creation Report Progress

My status bar is currently updated using 2 classes:

  • an INotifyPropertyChanged class called StatusBar
  • a separate class called Status.

The StatusBar class:

public class StatusBar : INotifyPropertyChanged
{
    private string _message;
    private string _submessage;
    private bool _isindeterminate;
    private bool _visible;
    private int _min;
    private int _max;
    private double _progress;
    public event PropertyChangedEventHandler PropertyChanged;

    public StatusBar() { }

    public int Min
    {
        get { return this._min; }
        set
        {
            this._min = value;
            this.OnPropertyChanged("Min");
        }
    }
    public int Max
    {
        get { return this._max; }
        set
        {
            this._max = value;
            this.OnPropertyChanged("Max");
        }
    }
    public double Progress
    {
        get { return this._progress; }
        set
        {
            this._progress = value;
            this.OnPropertyChanged("Progress");
        }
    }
    public string Message
    {
        get { return this._message; }
        set
        {
            this._message = value;
            this.OnPropertyChanged("Message");
        }
    }

    public string SubMessage
    {
        get { return this._submessage; }
        set
        {
            this._submessage = value;
            this.OnPropertyChanged("SubMessage");
        }
    }

    public bool IsIndeterminate
    {
        get { return this._isindeterminate; }
        set
        {
            this._isindeterminate = value;
            this.OnPropertyChanged("IsIndeterminate");
        }
    }

    public bool Visible
    {
        get { return this._visible; }
        set
        {
            this._visible = value;
            this.OnPropertyChanged("Visible");
        }
    }

    void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }

The Status class:

public class Status
{
    public static void Initialise(StatusBar status, string _message, string _submessage, int _min, int _max, double _progress, bool _visible, bool _isindeterminate)
    {
        status.Visible = _visible;

        if (_isindeterminate == false)
        {
            status.Message = _message;
            status.SubMessage = _submessage;
            status.Progress = _progress;
            status.Min = _min;
            status.Max = _max;
            status.IsIndeterminate = _isindeterminate;
        }
        else
        {
            status.Message = _message;
            status.IsIndeterminate = _isindeterminate;
        }
    }

    public static void UpdateProgress(StatusBar status, double _progress)
    {
        if (_progress == status.Max)
        {
            Complete(status);
        }
        else
        {
            status.Progress = _progress;
        }
    }

    public static void UpdateMessage(StatusBar status, string _message)
    {
        status.Message = _message;
    }

    public static void UpdateSubMessage(StatusBar status, string _submessage)
    {
        status.SubMessage = _submessage;
    }
    public static void UpdateProgressMessage(StatusBar status, string _message, double _progress)
    {
        status.Message = _message;
        status.Progress = _progress;
    }

    public static void Complete(StatusBar status)
    {
        status.Message = "Ready";
        status.SubMessage = " ";
        status.Min = 0;
        status.Max = 0;
        status.Progress = 0;
        status.Visible = false;
        status.IsIndeterminate = false;
    }
}

For creating the zip file and updating the status bar I currently have the following:

string sourceDirectory = "C:\\Access\\Test";
string archive = @"C:\Access\Test.zip";

Status.Initialise(statusbar, "Creating Zip File...", "", 0, 100, 0, true, false);

Backup.CreateFromDirectory(sourceDirectory, archive,
        new BasicProgress<double>(p => Status.UpdateProgressMessage(statusbar, $"{p:P2} archiving complete", p)));

The code above is working and the Zip file is created.
However the progress is only reported once the process has been completed and not during the process.

How can I change this to update the progress during the zip file creation?

Jimi
  • 29,621
  • 8
  • 43
  • 61
Rhysf93
  • 43
  • 1
  • 8
  • Debug your code to figure out what parts of your code are executed when exactly, and how the the values/state of the `StatusBar` instance change precisely. Also double-check how you bind your WPF user interface against the `StatusBar` instance, and make sure that your code only uses **only one and the same** StatusBar instance for both modifying the current status and the WPF UI bindings. (Side note: The functionality of the `Status` class should naturally be part of the `StatusBar` class. Also, `StatusBar` is a misleading class name here, as a "status bar" usually denotes a UI element) –  Dec 14 '18 at 13:37
  • The binding is correct, and there is only one instance of the `StatusBar` created in my application and it is passed to windows when new ones are created. It does update the statusbar with the progress but only at the end of the operation. – Rhysf93 Dec 14 '18 at 13:45
  • 1
    Make sure that your call of `Backup.CreateFromDirectory` is _really_ being executed asynchronously in a background thread instead of being executed in the main UI thread (which would block the UI thread and thus prevent the UI from updating until Backup.CreateFromDirectory completes). I cannot say anything more, because your question and the code therein do not reveal enough information about possible causes for your problem. Your best way forward is using the debugger to navigate/step through your code to see and understand what is really going on there... –  Dec 14 '18 at 13:51

0 Answers0