How to make a Progressbar run while a command is running

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 10 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
deiandrei
Posts: 11
Last visit: Mon Jul 11, 2022 4:55 am

How to make a Progressbar run while a command is running

Post by deiandrei »

Hi guys,

I have a script PowerShell Studio form with a script behind, which is searching the Active Directory for printer queues. So, when I hit the "Scan" button, it starts searching the AD and will then display the results in a datagridview.
My problem is that this scan might take some 10-15 seconds, maybe even more, depending on the connection speed.
Therefore I added a ProgressbarOverlay and set the "Visible" property to $false and the "Style" to Marquee
What I want to achieve: when I press the "Scan", the ProgressbarOverlay should appear and run until the command has finished. Then disappear again.
This is what I tried so far:
$buttonScan_click={
$progressbaroverlay1.visible = $true
Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | foreach-object {$datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName)}
$progressbaroverlay1.visible = $false
}

In this case, the progressbaroverlay does not even appear (because the $true and $false commands are executed both at the end).
When I delete the last row, which is setting "Visible" to $false, then the progressbar appears, but only after the scan has finished and the datagridview has been populated. But it's too late. I need it to appear before the Get-ADObject command and disappear afterwards.
Does anyone have any ideas?
User avatar
deiandrei
Posts: 11
Last visit: Mon Jul 11, 2022 4:55 am

Re: How to make a Progressbar run while a command is running

Post by deiandrei »

I figured it out!
It was because the form wasn't refreshing, so I had to do it like this:

$buttonScan_click={
$progressbaroverlay1.visible = $true
$MainForm.Refresh()
Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | foreach-object {$datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName) | Foreach-Object {$MainForm.Refresh()}}
$progressbaroverlay1.visible = $false
}

But take care! The Refresh for each object will slow down the search a lot!! Instead of taking 5 seconds to search, it took me about 30-40 seconds.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to make a Progressbar run while a command is running

Post by jvierra »

Try refreshing just the progress bar.
User avatar
deiandrei
Posts: 11
Last visit: Mon Jul 11, 2022 4:55 am

Re: How to make a Progressbar run while a command is running

Post by deiandrei »

I just tried it and the search process is working really fast, but the refresh of the progressbar alone is making the progressbar blinking and lagging.

Nevertheless, thanks for your idea! Appreciate it!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to make a Progressbar run while a command is running

Post by jvierra »

This is likely due to your tying to refresh the control in a single even rather than on an event.

Use an event to set the pbar value. If you are updating the grid then pace the increment code in the grid change event and the form and controls should run more smoothly.
User avatar
deiandrei
Posts: 11
Last visit: Mon Jul 11, 2022 4:55 am

Re: How to make a Progressbar run while a command is running

Post by deiandrei »

I am not sure I can follow you.
I am still at my beginning in scripting.
Maybe a simple example from your side would enlighten me.
I don't know what you mean by bpar and pacing the increment code in the grid change event.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to make a Progressbar run while a command is running

Post by jvierra »

The following is all you can do with the button without causing trouble.
PowerShell Code
Double-click the code block to select all.
$buttonScan_click={
    $progressbaroverlay1.visible = $true
    Get-ADObject -filter {objectclass -eq "printQueue"} -Searchbase $Searchbase -Properties serverName,printerName,Location,Description,driverName | 
        foreach-object{
            $datagridview1.Rows.Add($_.serverName,$_.printerName,$_.Location,$_.Description,$_.driverName)
        }
    $progressbaroverlay1.visible = $false
}
In the $datagridview :onchaange" or "onupdate you might be able to update the progress bar.

Start by reading the blog here on the new custom progressbaroverlay control. Once you are clear as to how it works I suspect you will see how to do it.

The datagridview is being updated a row at a time. The pbar(prograssbar) is not being updated. It will not display until it gets the first update message. That should happen on the update event of the datagridview.

YOu might also read the blogs on how to build forms. It will help you to understand how forms work. They are driven by events and not code. Code pretty much just sets the stage for how the form behaves.
User avatar
deiandrei
Posts: 11
Last visit: Mon Jul 11, 2022 4:55 am

Re: How to make a Progressbar run while a command is running

Post by deiandrei »

Alright sir, thank you for your time and effort !
As I said, I am still at the beginning, but I ensure you that I will progress with Powershell and Powershell Studio!

Have a nice day!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to make a Progressbar run while a command is running

Post by jvierra »

Here is a complete working example. The ProgressBarOverlay is already fully evented and it runs very nicely. We just need to feed the grid a row at a time

I have places a timer in to synthesize waits so you can actually see the bar progress. The process is restartable and listens for events so the stop button will work.

Run it. Along with the links I posted you will be able to better understand how it works.

File attached:
Attachments
Demo-ProgressBar.zip
Demo-ProgressBarOverlay
(2.61 KiB) Downloaded 2592 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: How to make a Progressbar run while a command is running

Post by davidc »

I recommend reading the following blog articles:

Creating Responsive Loops:

http://www.sapien.com/blog/2011/07/15/p ... ive-loops/


Creating Responsive Forms:

http://www.sapien.com/blog/2012/05/16/p ... ive-forms/

David
David
SAPIEN Technologies, Inc.
This topic is 10 years and 10 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