0

Im creating a simple powershell GUI using WPF like this one... WPF GUI to powershell

The problem is that the textbox where i show logs is not being updated while the powershell script is executing some commands. The same function that updates the text of the textbox also logs to standard output, but the messages are shown in the stdout immediately whereas the ones on the textbox are not shown until the end.

$MyTextbox.AddText($LogsLine) -> Not shown until the end

Write-Host ($LogsLine) -> Shown immediately

Is there any way to refresh the textbox after updating its "text" value?

Thanks!

I have tried looking for methods of the textbox or the form itself but i do not find a refresh function.

JSP
  • 1
  • 1
  • https://stackoverflow.com/q/818911/45375 discusses refreshing of WPF controls in general. Is the PowerShell code run from an event handler? – mklement0 Jan 27 '23 at 16:20
  • Yes, after a button click – JSP Jan 27 '23 at 16:33
  • 1
    You would need to run whatever those commands do on a background thread. I have no idea how you do that with powershell though. I think your problem is they run on the UI thread and hence it's busy doing stuff until they are finished. Meaning it cannot render any of your changes. – Andy Jan 27 '23 at 16:43
  • Maybe https://learn.microsoft.com/en-us/powershell/module/threadjob/start-threadjob?view=powershell-7.3 – Andy Jan 27 '23 at 16:44
  • If the PowerShell script is long-running, it will block WPF updates. https://stackoverflow.com/a/65531439/45375 shows how to use background jobs to avoid freezing the GUI. Conceivably, as a simpler alternative, you could lift the `DoWpfEvents` from there and call it periodically while your PowerShell code runs. – mklement0 Jan 27 '23 at 16:46
  • [System.Windows.Forms.Application]::DoEvents() – JSP Jan 27 '23 at 16:52
  • Using that after updating the textbox did the trick. – JSP Jan 27 '23 at 16:53
  • Glad to hear it, @JSP, but I'm confused: That method relates to _WinForms_, not WPF. Are you saying that it also worked in your WPF window - or is your GUI WinForms-based, after all? – mklement0 Jan 27 '23 at 16:55
  • Sorry. Maybe im completely wrong by saying its WPF. I dont have exp with powershell GUI and with GUI development in general. What im using is similar to this https://stackoverflow.com/questions/49881505/wpf-gui-to-powershell – JSP Jan 27 '23 at 17:27
  • The linked post is indeed WPF. An easy way to determine what you're using in practice is: if your form is created with something like `New-Object System.Windows.Forms.Form` or `[System.Windows.Forms.Form]::new()` or `[Form]::new()`, you're using WinForms. Something like `[System.Markup.XamlReader]::Load()` implies WPF. – mklement0 Jan 27 '23 at 17:32
  • 1
    For any posts where people are trying to figure out how to create a GUI for a PowerShell script I like to refer them to [FoxDeploy's Learning GUI Toolmaking series](https://www.foxdeploy.com/series/LearningGUIs). I learned a lot from it when I needed to make a GUI for a script, and he periodically updates it with more info so it's worth checking back now and then. – TheMadTechnician Jan 27 '23 at 18:12

0 Answers0