Pause script with progress bar

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Pause script with progress bar

Post by Lery154 »

I've been working with the progress bar overlay control. I've been able to get it to do what I need for about 95% of my script. I'm stuck on the following requirement. My form is made up of several functions. When an action is performed a function is usually called. Some of the functions have a requirement to pause for about 20 seconds. During the pause I would like the progress bar overlay to start progressing from left to right, completing when the 20 seconds are finished.

Can anyone provide an example on how to pause a script while keeping the progress bar moving for the 20 seconds? If the bar cannot animate, because the script is paused, I understand. In that case, what are my alternatives?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pause script with progress bar

Post by jvierra »

Place the progressbar update in a loop.

1..20 | %{
update progress bar here
sleep 1
}
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Pause script with progress bar

Post by Lery154 »

jvierra wrote:Place the progressbar update in a loop.

1..20 | %{
update progress bar here
sleep 1
}
That did it. I had to change the step value to 5. Otherwise it was pausing for 20 seconds, but the progress bar was full within 10 seconds. Is that the correct approach?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pause script with progress bar

Post by jvierra »

If the total is 20 and the increment is one (step 1) If the range is 100 then step 5 for 20 seconds (20 * 5 = 100)
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Pause script with progress bar

Post by Lery154 »

Ok I'm making progress, but I'm not able to get the textoverlay method to stick. I can get it to work in other functions in the form, just not this one. The progress bar text changing from the default Ready status to "Pausing for 20 seconds to allow start request to complete" and then does not go back to Ready. It's making me pull more of my hair out of my head :-) Especially when it works just fine in other functions.


Here it is:
PowerShell Code
Double-click the code block to select all.
button_Start_click{

$progressbaroverlay1.TextOverlay = "Pausing for 20 seconds to allow start request to complete..."
			1..20 | %{
					$service = get-service -ComputerName $ComputerName -servicename 'Bits'
	 				$startservice = Start-Service -InputObject $service
					$progressbaroverlay1.Step = 5
    				$progressbaroverlay1.PerformStep()
					start-Sleep 1			
					}
$progressbaroverlay1.Value = 0
$pregressbaroverlay1.textoverlay = "Ready"

}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Pause script with progress bar

Post by davidc »

Try this after setting the text:
PowerShell Code
Double-click the code block to select all.
$progressbaroverlay1.Refresh()


David
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Pause script with progress bar

Post by davidc »

Or set the value to 0 after changing the text. We will look at making sure the control refreshes after setting the text.

David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pause script with progress bar

Post by jvierra »

Here is the whole thing including the math:
PowerShell Code
Double-click the code block to select all.
$button_start_Click={
	$max=100
	$seconds=20
	$tlen=(20/100) * 1000 #milliseconds
	$progressbaroverlay1.Maximum=$max
	$progressbaroverlay1.Step=1
	$progressbaroverlay1.TextOverlay = "Pausing for 20 seconds to allow start request to complete..."
	1..$max | %{
			#$service = get-service -ComputerName $ComputerName -servicename 'Bits'
			#$startservice = Start-Service -InputObject $service
			$progressbaroverlay1.PerformStep()
			start-Sleep -Milliseconds $tlen
			}
	$progressbaroverlay1.TextOverlay='Ready'
	$progressbaroverlay1.Value=0
}
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Pause script with progress bar

Post by Lery154 »

Prior to posting I did try $progressbaroverlay1.Refresh() and $progressbaroverlay1.Value = 0 It did not work. Which I found odd, gave up and came here for help :-)

jvierra's sample worked perfectly. I see no difference between what you did and I did. With the exception of the math, but I was going to fix that. Also, using your math it takes the progress roughly 50 seconds to fill. Instead of 20.
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Pause script with progress bar

Post by Lery154 »

The other crazy thing. Using the code posted by jvierra and simply adding $progressbaroverlay1.TextOverlay = "Stoping service..." Does not work.

Take this code, which works:
PowerShell Code
Double-click the code block to select all.
$max=100
    	$seconds=20
    	$tlen=(20/100) * 1000 #milliseconds
    	$progressbaroverlay1.Maximum=$max
    	$progressbaroverlay1.Step=1
    	$progressbaroverlay1.TextOverlay = "Pausing for 20 seconds to allow start request to complete..."
    	1..$max | %{
            $service = get-service -ComputerName $ComputerName -servicename 'Bits'
            $startservice = Start-Service -InputObject $service
            $progressbaroverlay1.PerformStep()
            start-Sleep -Milliseconds $tlen
            }
    $progressbaroverlay1.TextOverlay='Ready'
    $progressbaroverlay1.Value=0
and change it to:
PowerShell Code
Double-click the code block to select all.
$progressbaroverlay1.TextOverlay = "Starting service..."
		$max=100
    	$seconds=20
    	$tlen=(20/100) * 1000 #milliseconds
    	$progressbaroverlay1.Maximum=$max
    	$progressbaroverlay1.Step=1
    	$progressbaroverlay1.TextOverlay = "Pausing for 20 seconds to allow start request to complete..."
    	1..$max | %{
            $service = get-service -ComputerName $ComputerName -servicename 'Bits'
            $startservice = Start-Service -InputObject $service
            $progressbaroverlay1.PerformStep()
            start-Sleep -Milliseconds $tlen
            }
    $progressbaroverlay1.TextOverlay='Ready'
    $progressbaroverlay1.Value=0
$progressbaroverlay1.TextOverlay = "Starting service..." will not display. It just goes directly to $progressbaroverlay1.TextOverlay = "Pausing for 20 seconds to allow start request to complete..."
This topic is 10 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked