2

im building a tool with powershell wpf.

I finally managed to get my head around runspaces somewhat and am successfully updating the main forms progress bar from a runspace, woo!

However, it is really slow when i touch anything in the sync hash table it seems. Without updating a label &/or the progress bar in the synchash the function completes WAY faster. Am I missing something still?

My Runspace code is basically this:

$SyncHash = [hashtable]::Synchronized(@{
ProgressBar = $ProgressBar
Window      = $Window
})

$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"         
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash", $syncHash)          
$SessionScript = [powershell]::Create().AddScript( { 
    for ($i = 0; $i -lt 100 ; $i++) {
        Start-Sleep -Seconds .1
        $SyncHash.Progress.Dispatcher.Invoke([action] { $SyncHash.Progress.Value = $i })
    }
})
$SessionScript.Runspace = $Runspace
$SessionScript.BeginInvoke()

So for example if i comment out the line to update the progress bar that will complete way faster.... whyyy????

bdot
  • 51
  • 4

0 Answers0