Write PowerShell Output (as it happens) to WPF UI Control

三世轮回 提交于 2019-11-29 15:47:23

Out of curiosity, have you considered that instead of PowerShell firing up WPF and displaying some output, it should be the other way around?

Perhaps you should be invoking PowerShell from within a WPF application, and capturing the output to be displayed?

http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

iRon

I ran into the same question and seen quiet some suggestions that either refer to C# classes or examples where the UI is placed in the worker instead of the actual worker task. Anyway, after spending quite some time I figured it out:

Add-Type -AssemblyName PresentationFramework
$Xaml = New-Object System.Xml.XmlNodeReader([XML]@"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" WindowStartupLocation = "CenterScreen" ShowInTaskbar = "True">
    <Grid>
        <TextBox x:Name = "TextBox"/>
    </Grid>
</Window>
"@)

Function Start-Worker {
    $TextBox = $Window.FindName("TextBox")
    $SyncHash = [hashtable]::Synchronized(@{Window = $Window; TextBox = $TextBox})
    $Runspace = [runspacefactory]::CreateRunspace()
    $Runspace.ThreadOptions = "ReuseThread"         
    $Runspace.Open()
    $Runspace.SessionStateProxy.SetVariable("SyncHash", $SyncHash)          
    $Worker = [PowerShell]::Create().AddScript({
        for($Progress=1; $Progress -le 10; $Progress++){
            $SyncHash.Window.Dispatcher.Invoke([action]{$SyncHash.TextBox.AppendText($Progress)})
            Start-Sleep 1                                                       # Some background work
        }
    })
    $Worker.Runspace = $Runspace
    $Worker.BeginInvoke()
}

$Window = [Windows.Markup.XamlReader]::Load($Xaml)                              # Requires -STA mode
$Window.Add_Loaded({Start-Worker})
[Void]$Window.ShowDialog()

For a windows control example see: PowerShell: Job Event Action with Form not executed

I made some adjustments in your code. I wouldn't run your For loop within the dispatcher and instead would run the dispatcher block in the For loop. I tested and it didn't seem to lock up for me (had some moments where the performance wasn't the best, but that was expected) and kept writing to the window.

for ($i = 0; $i -lt 10000; $i++) {
    $uiHash.OutputTextBox.Dispatcher.Invoke("Normal", [action]{
        $uiHash.OutputTextBox.AppendText("hi")
    })
}

This may be a little late but try downloading WPFRunspace , which provides a Powershell backgroundworker cmdlet for WPF and Win Forms GUI scripts that will execute in the background and update your GUI either as output is received or on completion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!