繁体   English   中英

C#中的Powershell / C#Streams.Progress.DataAdded EventHandler无法按预期工作

[英]Powershell/C# Streams.Progress.DataAdded EventHandler in C# not working as expected

下面是我正在运行的问题的示例。这个简单的脚本将计数为5,并将进度记录添加到数据集合中时将触发Progress DataAdded事件。

void Button_Click(object sender, RoutedEventArgs e)
{
PowerShell ps1 = PowerShell.Create();
ps1.Streams.Progress.DataAdded += new EventHandler<DataAddedEventArgs>(Progress_DataAdded); // Set the event handler when data is added to the progress collection
ps1.AddScript("for($i = 1 ; $i -le 5 ; $i++) {Write-Progress -Activity Count -status $i ; sleep 1}"); // Simple script that will count to 5 and generate the progress activity
ps1.BeginInvoke();
}

void Progress_DataAdded(object sender, DataAddedEventArgs e)
{
listBox1.Items.Add(((PSDataCollection<ProgressRecord>)sender)[e.Index].ToString()); // Add the progress records to a listbox on the UI, this doesn't work
// MessageBox.Show(((PSDataCollection<ProgressRecord>)sender)[e.Index].ToString()); // Display the progress records in a messagebox, this does work
}

启用MessageBox并禁用Listbox时,它将起作用,并在消息框中显示进度记录。 当MessageBox被禁用而Listbox被启用时(如上所述),它不起作用,记录将不会显示在listbox中。 可能与当前线程的运行方式有关,也可能与Powershell脚本的调用方式有关。 希望有人能让我走上正确的道路,我要做的就是在执行脚本时在UI的列表框中显示进度记录。 提前致谢!

这个例子对我来说似乎很好。 它将适当的文本打印到Visual Studio Express 2013 Windows版的“ Output窗口中。 我相当确定您所看到的问题的根本原因是您在调用BeginInvoke()而不是Invoke() 当我使用BeginInvoke()在下面测试示例代码时,它没有将五(5)条额外的行打印到Debug输出中。 只有Progress.Add()方法成功导致事件处理程序被调用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Diagnostics;

namespace PowerShellTest01
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Create PowerShell instance
            var ps = PowerShell.Create();
            // 2. Add C# anonymous method to respond to event
            ps.Streams.Progress.DataAdded += delegate(object sender, DataAddedEventArgs e) { Debug.WriteLine("Data was added to progress stream"); };
            // 3. Add a new ProgressRecord instance
            ps.Streams.Progress.Add(new ProgressRecord(5, "test", "test"));
            // 4. Add a PowerShell script that calls Write-Progress
            ps.AddScript("1..5 | % { Write-Progress -Activity 'My Important Activity' -PercentComplete ($PSItem*20) -Status 'This is my status'; Start-Sleep -Milliseconds 200; }");
            // 5. Invoke the script synchronously
            ps.Invoke();
            // 6. Wait for user input
            Console.Read();
        }
    }
}

更新 :包括演示将对象添加到ListBox

void Progress_DataAdded(object sender, DataAddedEventArgs e)
{
    Debug.WriteLine("sender type is: " + sender.GetType());
    var records = (PSDataCollection<ProgressRecord>)sender;
    Debug.WriteLine("Progress stream contains {0} records", records.Count);
    ListProgressRecords.Items.Add(records[e.Index]);
}

PowerShell:ProgressRecords

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM