Problem with Progress Bar Overlay

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 5 years and 1 month 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
mat.nolen
Posts: 2
Last visit: Tue Dec 12, 2023 11:59 am

Problem with Progress Bar Overlay

Post by mat.nolen »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: Power Shell Studio 2019 version 5.6.157
32 or 64 bit version of product: 64 bit
Operating system: Windows 10 Pro
32 or 64 bit OS: 64 bit

*** Please add details and screenshots as needed below. ***

Testing out the Progress Bar with it updating after checking each file. I have found when I run the script it will load the progress bar and end at 100% but there is still empty space in the progress bar. Almost like it does not understand it should be done. I have tried to change the overlay, step and value but still can't get it to work. Below is a copy of the code. The test is just creating a folder on the desktop then confirming. Does not like the progress bar. I also attached the form. Any ideas????



$form1_Load={
#TODO: Initialize Form Controls here
if (-not (Test-Path "C:\Source\ProgressTesting")) { New-Item -ItemType dir "C:\Source\ProgressTesting" -Force }
New-Item 'C:\Source\ProgressTesting\file1.txt' -Force
New-Item 'C:\Source\ProgressTesting\file2.txt' -Force
New-Item 'C:\Source\ProgressTesting\file3.txt' -Force
New-Item 'C:\Source\ProgressTesting\file4.txt' -Force
}

$buttonRun_Click={
#TODO: Place custom script here
$richtextbox1.Clear()
$files = Get-Item "C:\Source\ProgressTesting\*"

$progressbaroverlay1.Maximum = $files.count
$progressbaroverlay1.Step = 1
$progressbaroverlay1.Value = 0

foreach ($file in $files)
{
$progressbaroverlay1.PerformStep()
$progressbaroverlay1.Refresh()
$richtextbox1.AppendText("$file`n")
$richtextbox1.Refresh()
start-sleep 2
}
}
Attachments
ProgressBarOverlay.zip
(163.21 KiB) Downloaded 163 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Problem with Progress Bar Overlay

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
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: Problem with Progress Bar Overlay

Post by jvierra »

What is the Maximum? Be sure you are counting to the maximum and not one short.

You shouldn't need "refresh".
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Problem with Progress Bar Overlay

Post by davidc »

This can occurs because the progress bar is animated. When the pipeline is stopped (sleep command), it cannot animation.

You could look into using the Job Tracker control set. So that the work is not holding up the GUI:

https://info.sapien.com/index.php/guis/ ... sive-forms
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: Problem with Progress Bar Overlay

Post by jvierra »

Sleep should not effect control method calls as they happen directly. That is why the "PerformStep()" is added to the progress controls.

I have had this issue without sleep and it always seems to be bad arithmetic. If the range is set to file count and the increment is 1 then the minimum should be set to 1. 1 to count is the range. 0 to count is the range - 1.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with Progress Bar Overlay

Post by jvierra »

Actually David is correct. With the Start-Sleep the control is always one behind. Either process in a timer tick or use a jobControl.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with Progress Bar Overlay

Post by jvierra »

The following stays in sync after the first item but ends correctly.

Code: Select all

$buttonRun_Click={
	#TODO: Place custom script here
	$richtextbox1.Clear()
	$files = Get-Item "C:\Source\ProgressTesting\*"
		
	$progressbaroverlay1.Maximum = $files.count - 1
    $progressbaroverlay1.Minimum = 0
	$progressbaroverlay1.Step = 1
	
	$files |
        ForEach-Object{
    		start-sleep 1
            $i++
    		$richtextbox1.Lines += $_
            $progressbaroverlay1.PerformStep()
            [System.Windows.Forms.Application]::DoEvents()
    	}
    $richtextbox1.Lines += 'Complete!'
}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Problem with Progress Bar Overlay

Post by mxtrinidad »

Hum! I posted my comment and didn't send it.

In his original code, he was only missing the "$progressbaroverlay1.Step = 1" line within the foreach loop, and that would have solve the issue of the bar not updating. Of course the *.refresh() method is not needed.

Of course, there are so many other ways to fix it.
:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with Progress Bar Overlay

Post by jvierra »

mxtrinidad wrote: Tue Feb 26, 2019 4:49 pm Hum! I posted my comment and didn't send it.

In his original code, he was only missing the "$progressbaroverlay1.Step = 1" line within the foreach loop, and that would have solve the issue of the bar not updating. Of course the *.refresh() method is not needed.

Of course, there are so many other ways to fix it.
:)
No. It was in the zip he uploaded and the issue still existed. B setting the max ot count -1 or the min to 1 we avoid the ending shortage but the start is one behind until the event ends.

It is interesting that this doesn't happen in C#. It is an artifact of the form running on the same thread as the message loop. In C# the events and code run on one thread and the form and controls run on the main thread. When we use Application.Run(from) the Run sets up the way the event code is run and the message loop calls delegates that dispatch to the controls. This is not possible in PowerShell. We cannot use form.Show() or Application.Run() in a single threaded program. PowerShell can only display modal dialogs.

This limitation in PowerShell is why users should not consider using PS and forms as an application development tool. Many of the required behaviors in a compiled form cannot be accessed from a script and a packaged script is not a compiled application but is a PowerShell host executing an embedded script. It is still a single threaded PowerShell engine instance.

David's reference to JobTracker is one easy way to manage a ProgressBar. We can also use a runspace and synchash to directly update the progress in real time correctly.
User avatar
mat.nolen
Posts: 2
Last visit: Tue Dec 12, 2023 11:59 am

Re: Problem with Progress Bar Overlay

Post by mat.nolen »

Thanks for all your suggestions. I"m going to read through the post and try a few of the options. Seems like you can build this out many different ways. I followed an older sapien document for the example.
This topic is 5 years and 1 month 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