views:

343

answers:

1

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?

+4  A: 

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

Richard Berg
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
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