1

I'm currently creating a WPF form which retrieve some manual information and then perform some activities through PowerShell.

Activity is a class that implement INotifyPropertyChanged in order to reflect property changes (for example when an activity fails, its status changes from "running" to "Error"). I've created a listView (datagrid) which itemsSource is an ObservableCollection named Sequence. Sequence is declared in mainWindows class.

Activity Class is that :

public class Activity : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int step;
    public int Step
    {
        get { return this.step; }
        set
        {
            if (this.step != value)
            {
                this.step = value;
                this.NotifyPropertyChanged("Step");
            }
        }
    }

    private String group;
    public String Group
    {
        get { return this.group; }
        set
        {
            if (this.group != value)
            {
                this.group = value;
                this.NotifyPropertyChanged("Group");
            }
        }
    }

    private String activityName;
    public String ActivityName
    {
        get { return this.activityName; }
        set
        {
            if (this.activityName != value)
            {
                this.activityName = value;
                this.NotifyPropertyChanged("ActivityName");
            }
        }
    }

    private Model.Status status;
    public Model.Status Status
    {
        get { return this.status; }
        set
        {
            if (this.status != value)
            {
                this.status = value;
                this.NotifyPropertyChanged("Status");
            }
        }
    }

    private String cmdlet; 
    public String Cmdlet
    {
        get { return this.cmdlet; }
        set
        {
            if (this.cmdlet != value)
            {
                this.cmdlet = value;
                this.NotifyPropertyChanged("Cmdlet");
            }
        }
    }

    private Dictionary<String, Object> parameters;
    public Dictionary<String, Object> Parameters
    {
        get { return this.parameters; }
        set { this.parameters = value; }
    }



    public Activity(int _Step, String _ActivityName, String _Group , Model.Status _Status, String _Cmdlet, Dictionary<String, Object> _Parameters)
    {
        this.Step = _Step;
        this.Group = _Group;
        this.ActivityName = _ActivityName;
        this.Status = _Status;
        this.Cmdlet = _Cmdlet;
        this.Parameters = _Parameters;
    }

    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    internal void Run(ref Runspace _rs)
    {
        using (PowerShell ps = PowerShell.Create())
        {
            ps.Runspace = _rs;
            //ps.AddCommand(this.cmdlet).AddParameter("Site", this._Selection.SelectedSite);
            ps.AddCommand(this.cmdlet).AddParameters(this.Parameters);

            Collection<PSObject> results = ps.Invoke();
            if (results.Count > 0 && ps.HadErrors == false)
            {
                foreach (PSObject item in results)
                {
                    this.status = (Model.Status)Enum.Parse(typeof(Model.Status), item.ToString(), true);
                }
            }
            else
            {
                this.Status = Model.Status.Error;
            }
        }
    }
}

Deployment start when I click on the Last "Next" button. A function Deploy is started :

private void DeploySite()
    {
        // Build list of activities
        // 
        int i = 0;
        Dictionary<String, Object> _params = new Dictionary<string,object>(); 
        _params.Add("Site", this._Selection.SelectedSite);
        this.Sequence.Add(new Model.Activity(i++, "Update System Discovery Method", "Configure Administration Node", Model.Status.NoStatus, "Update-SystemDiscoveryMethod", _params));
        this.Sequence.Add(new Model.Activity(i++, "Update User Discovery Method", "Configure Administration Node", Model.Status.NoStatus, "Update-SystemDiscoveryMethod", _params));


        // Start Execution of activities
        //
        foreach (Model.Activity activity in this.Sequence)
        {
            activity.Status = Model.Status.Running;
            activity.Run(ref rs);
        }
    }

My problem is that the view is updated only when every activities are finished, in other words when the DeploySite() function ends.

I would like to view progression of each activities when there are executed.

Any help will be greatly appreciated.

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
Régis
  • 233
  • 1
  • 9

2 Answers2

0

Since you are continuously doing work in the UI thread in DeploySite(), there is no chance for the View to do anything. You should do the work in a background thread, and only do the UI updates themselves in the UI thread.

Daniel Rose
  • 17,233
  • 9
  • 65
  • 88
0

Run your activities in a new thread an dispatch results/updates to the UI thread. Current implementation is blocking the UI, as mentioned by @Daniel Rose.

Here, I hope this meta code helps you solve it.

private void DeploySite()
{
    // Fire up a new Task
    Task.Run(() =>
        {
            for (int i = 0; i < 10; i++)
            {
                int i1 = i;
                // Dispatch
                System.Windows.Application.Current.Dispatcher.BeginInvoke(
                    new Action(() => Sequence.Add(i1)), null);
                Thread.Sleep(100);
            }
        });
}
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62