我目前正在创建一个WPF表单,它可以检索一些手动信息,然后通过PowerShell执行一些活动。
Activity是一个实现INotifyPropertyChanged以反映属性更改的类(例如,当活动失败时,其状态从"运行"到"错误")。 我创建了一个listView(datagrid),其中itemsSource是一个名为Sequence的ObservableCollection。 Sequence在mainWindows类中声明。
活动类是:
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;
}
}
}
}
当我点击Last&#34; Next&#34;按钮。 启动功能部署:
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);
}
}
我的问题是视图仅在每个活动完成时更新,换句话说,当DeploySite()函数结束时。
我希望在执行时查看每个活动的进展情况。
非常感谢任何帮助。
答案 0 :(得分:0)
由于您在DeploySite()
中的UI线程中不断进行工作,因此View无法执行任何操作。您应该在后台线程中完成工作,并且只在UI线程中自己更新UI。
答案 1 :(得分:0)
在新线程中运行您的活动,以及对UI线程的调度结果/更新。正如@Daniel Rose所提到的,当前的实现阻止了UI。
在这里,我希望这个meta
代码可以帮助您解决问题。
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);
}
});
}