Continuous Progress Bar

Hi,

I would like a continuously looping progress bar for the duration of my code. I’m having some success in getting a job to loop until the condition is met. I understand that you need to use Receive-Job to get the jobs output though, but this requires me to use -Wait, which stops it getting to the point where the condition changes value, causing the progress to never stop:

[pre]

$completed = $false
$i = 0

Start-Job -ScriptBlock {

Do {

$i++
Write-Progress -Activity “Test” -PercentComplete $i

if ($i -eq 100) {

$i = 0

}

} While ($completed -ne $true)

}

Start-Sleep -Seconds 5

$completed = $true

Get-Job | ForEach {

Stop-Job -Id $.Id
Remove-Job -Id $
.Id

}

[/pre]

I’ve also had a play with Workflows with Parallel but because of the variable scoping issue I gave up:

[pre]

Workflow Test {

$completed = $false

Parallel {

Sequence {

Start-Sleep -Seconds 5
$Workflow:completed = $true

}

Do {

$i++
Write-Progress -Activity “Progress Test” -PercentComplete $i
Write-Output “completed: $Workflow:completed”

if ($i -eq 100) {

$i = 0

}

} While ($Workflow:completed -ne $true)

}

}

Test
[/pre]

Any suggestions?

Thanks

Is there a specific reason you need the progress bar to run in a parallel job, rather than simply updating the progress bar as a step in your actual task? e.g.

$i=0 # set the initial increment
$t=30 # set the total

while ( $i -le $t) { # perform increments until the total is reached

    "increment = $i" # display the current increment
    $p=($i/$t)*100 # calculate the percentage of the total

    Write-Progress -Activity "Test" -PercentComplete $p # update the progress bar

    $P = [math]::Round($p) # round the percentage for display
    "completion = $P%" # display the percent complete
    $i++ # increment
    Start-Sleep -Milliseconds 500 # slow the loop

} #end while