简体   繁体   English

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

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

Below an example of the issue I'm running in. This simple script will count to 5 and will trigger the Progress DataAdded event when the progress records are added to the data collection. 下面是我正在运行的问题的示例。这个简单的脚本将计数为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
}

When MessageBox is enabled and Listbox disabled it works and it displays the progress records in a messagebox. 启用MessageBox并禁用Listbox时,它将起作用,并在消息框中显示进度记录。 When MessageBox is disabled and Listbox enabled (as above) it doesn't work, the records are not shown in the listbox. 当MessageBox被禁用而Listbox被启用时(如上所述),它不起作用,记录将不会显示在listbox中。 Maybe something to do with the current thread it runs in or by the way the powershell script is invoked. 可能与当前线程的运行方式有关,也可能与Powershell脚本的调用方式有关。 Hopefully someone can get me on the right track with this one, all I want is to display the progress records in a listbox on the UI when the script is executed. 希望有人能让我走上正确的道路,我要做的就是在执行脚本时在UI的列表框中显示进度记录。 Thanks in advance! 提前致谢!

This example seems to work fine for me. 这个例子对我来说似乎很好。 It prints the appropriate text to the Output window in Visual Studio Express 2013 for Windows Desktop. 它将适当的文本打印到Visual Studio Express 2013 Windows版的“ Output窗口中。 I am fairly certain that the root cause of the issue you're seeing is that you're calling BeginInvoke() instead of Invoke() . 我相当确定您所看到的问题的根本原因是您在调用BeginInvoke()而不是Invoke() When I tested my sample code below with BeginInvoke() , it did not print the five (5) additional lines to the Debug output. 当我使用BeginInvoke()在下面测试示例代码时,它没有将五(5)条额外的行打印到Debug输出中。 Only the Progress.Add() method successfully caused the event handler to be called. 只有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();
        }
    }
}

Update : Including code that demonstrates adding the object to a ListBox . 更新 :包括演示将对象添加到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