0

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() 

})
Jay Ross
  • 1
  • 1
  • 1
  • 1
    The technique show in [this answer](https://stackoverflow.com/a/65531439/45375) should work. – mklement0 Jan 19 '23 at 14:40
  • I suggest you try to adapt the linked answers to your needs, and if you get stuck, you can update your post with your new attempt and a description of what, specifically, prevents you from fully solving the problem. Note that the key elements are that you must switch from the blocking `.ShowDialog()` call to a non-blocking `.Show()` call, which then requires you to implement a loop in which you process WPF events periodically (via the `DoWpfEvents` helper function) while being able to perform tasks such as iteratively collection job output and updating the form with it. – mklement0 Jan 19 '23 at 17:12
  • Note that unless you @-mention someone, they are unlikely to see your comment. Only those that actively follow a post (by clicking the "Follow" link below the post) get notified regardless - which not many people do. – mklement0 May 10 '23 at 12:40
  • This isn't the right site for the kind of personalized assistance you're looking for - I suggest considering other forums or asking colleagues for help. As suggested before, if you make an earnest attempt to adapt the linked solution yourself and _then_ get stuck, and describe the _specific problem_, you stand a much better chance of finding help here. – mklement0 May 10 '23 at 12:42
  • Some links that may be helpful: [The tour](https://stackoverflow.com/tour) and [how to ask a good question](http://stackoverflow.com/help/how-to-ask). – mklement0 May 10 '23 at 12:44

0 Answers0