2

I have a PowerShell function to look in a directory for zip files, extract it, and rename the files. The function also changes the status bar item to update what file it is on. I noticed that when this runs the GUI would freeze.

How can I get the function run and update the GUI without it freezing?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • For a WPF background worker see: http://stackoverflow.com/questions/23142137/write-powershell-output-as-it-happens-to-wpf-ui-control/36716964#36716964 – iRon Apr 21 '16 at 07:59

1 Answers1

4

Don't do expensive operations on the UI thread -- use a background worker. Here's a tutorial: http://dotnetperls.com/backgroundworker-introduction

Richard Berg
  • 20,629
  • 2
  • 66
  • 86
  • Richard, have you tried it? I did with winforms. I had no luck because as a post from 2006 states 'the problem is that the worker thread doesn't have a runspace to run the command in'. So e.g. $w.add_DoWork({ $global:i = 1 }) doesn't assign 1 to global var. I would be curious how to assign it a runspace.. – stej Jan 26 '10 at 08:58
  • I meant doing the entire Powershell pipeline on the background thread, not just the zip operation. This is somewhat difficult if he's writing a GUI app purely in Powershell (using WPK or something) but that was very uncommon at the time, and still is for the most part. I assumed he had a normal WPF app that was delegating certain admin-like tasks to Posh. The other option is to use the Write-Progress stream built into Posh v2, e.g. http://blogs.msdn.com/mediaandmicrocode/archive/2009/12/28/write-progress-wpk.aspx – Richard Berg Jan 26 '10 at 16:49