How to get a -job output data concurrently without freezing the gui?

135 Views Asked by At

I have a script block that when I press a button in a window it gets me the output data back in to a textbox but the issue is that it only gives me the last lines in the output, my question is how can I get the output text concurrently without stopping and not freezing my gui in Powershell using a Wpf gui?


$Status_Button.Add_Click({
    $window = New-Object System.Windows.Window
    $window.Title = "Status"
    $window.Background = "#FF0C090C"
    $window.Width = 800
    $window.MaxWidth = 800 
    $window.MinWidth = 800
    $window.Height = 550
    $window.MaxHeight = 550
    $window.MinHeight = 550
    $textbox = New-Object System.Windows.Controls.TextBox
    $textbox.Width = 750
    $textbox.Height = 420
    $textbox.Background = "Black"
    $textbox.Foreground = "White"
    $textbox.AcceptsReturn = $True
    $textbox.AcceptsTab = $True
    $textbox.TextWrapping = "Wrap"
    $textbox.VerticalScrollBarVisibility = "Auto"
    $textbox.HorizontalScrollBarVisibility = "Auto"
    $textbox.IsReadOnly = $True
    $statusbutton = New-Object System.Windows.Controls.Button
    $statusbutton.Content = "Refresh"
    $statusbutton.Width = 55
    $statusbutton.Height = 35
    $statusbutton.Margin = "0,0,0,5"
    $statusbutton.VerticalAlignment = "Bottom"
    $statusbutton.HorizontalAlignment = "Center"
    $statusbutton.Add_Click({
        if($Streams_RadioButton.IsChecked){foreach($job in $jobstreams){if(Get-Job -ChildJobState Completed -HasMoreData:$false){Remove-Job -Job $job; $textbox.Text = "Download Completed..." | Out-String} elseif(Get-Job -State Running){$textbox.Text = Receive-Job -job $job -Keep | Out-String} else {$textbox.Text = "NO DOWNLOAD SESSION WAS FOUND..." | Out-String}}} 
        elseif($Movies_RadioButton.IsChecked){foreach($job in $jobmovies){if(Get-Job -ChildJobState Completed -HasMoreData:$false){Remove-Job -Job $job; $textbox.Text = "Download Completed..." | Out-String} elseif(Get-Job -State Running){$textbox.Text = Receive-Job -job $job -Keep | Out-String} else {$textbox.Text = "NO DOWNLOAD SESSION WAS FOUND..." | Out-String}}} 
         elseif ($TV_Shows_RadioButton.IsChecked){foreach($job in $jobtvs){if(Get-Job -ChildJobState Completed -HasMoreData:$false){Remove-Job -Job $job; $textbox.Text = "Download Completed..." | Out-String} elseif(Get-Job -State Running){$textbox.Text = "Downloading: '$($MediaListbox.SelectedItem)' Please Wait... " | Out-String; $textbox.Text = Receive-Job -job $job -Keep | Out-String} else {$textbox.Text = "NO DOWNLOAD SESSION WAS FOUND..." | Out-String}}}    
    })
    $grid = New-Object System.Windows.Controls.Grid
    $grid.Children.Add($textbox)
    $grid.Children.Add($statusbutton)
    $window.Content = $grid
    $window.ShowDialog() 

})
0

There are 0 best solutions below