Continuous Progress Bar

Welcome Forums General PowerShell Q&A Continuous Progress Bar

  • This topic has 1 reply, 2 voices, and was last updated 7 months ago by
    Senior Moderator
    .
Viewing 1 reply thread
  • Author
    Posts
    • #195710
      Participant
      Topics: 2
      Replies: 0
      Points: 25
      Rank: Member

      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:

      $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
      
      }
      
      

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

      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
      

      Any suggestions?

      Thanks

    • #195815
      Senior Moderator
      Topics: 5
      Replies: 133
      Points: 706
      Helping Hand
      Rank: Major Contributor

      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
Viewing 1 reply thread
  • The topic ‘Continuous Progress Bar’ is closed to new replies.