ProgressbarOverlay problems

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 6 years and 4 weeks 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
Richibm
Posts: 1
Last visit: Mon Jan 07, 2019 7:30 am

ProgressbarOverlay problems

Post by Richibm »

Product, version and build: Powershell Studio 2018 5.5.148.0
32 or 64 bit version of product: 64 bitt
Operating system: Windows Server 2016
32 or 64 bit OS: 64 bit

Good Afternoon.
Hopefully someone more knowledgable than me can help.
I am trying to write a gui which connects to our vCenter server(s) and executes a number of powerCLI commands when corresponding button is pressed.
All my codes is working fine BUT i cannot get this Progressbaroverlay to work. Some of my PowerCLI commands take several seconds to complete, I want a progress bar (or animated icon) to visualise that the command is executing.
I cannot get my head around how it works.
For Example :
This is my code :

Code: Select all

$buttonEveryVMDiskCapacity_Click={
	#TODO: Place custom script here
	$EveryVMdisk = ForEach ($VM in Get-VM) { ($VM.Extensiondata.Guest.Disk | Select @{ N = "Name"; E = { $VM.Name } }, DiskPath, @{ N = "Capacity(MB)"; E = { [math]::Round($_.Capacity/ 1MB) } }, @{ N = "Free Space(MB)"; E = { [math]::Round($_.FreeSpace / 1MB) } }, @{ N = "Free Space %"; E = { [math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)), 0) } }) }
	Update-DataGridView -DataGridView $datagridview1 -Item $EveryVMdisk -AutoSizeColumns AllCells
}
I know i need this :

Code: Select all

$progressbaroverlay1_Click={
	#TODO: Place custom script here
	
}
How do I get my code, and wrap it in the progressoverlay? I have read the help files on Sapien but I am still confused :(

Thank you in advance.

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

Re: ProgressbarOverlay problems

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS BY MODERATOR]

This article may help:

https://info.sapien.com/index.php/guis/ ... sive-loops
David
SAPIEN Technologies, Inc.
User avatar
TonySpeight
Posts: 5
Last visit: Tue Mar 14, 2023 4:57 am

Re: ProgressbarOverlay problems

Post by TonySpeight »

People may suggest creating a job and use the Update Job function within Powershell Studio.

I have a robocopy script which is has kind of similar commands which may help you.

I'm no expert in Powershell but I have been learning over the past few months. I have added a small sample of a script I have used.
  1. $progress = Receive-Job -Job $robocopyjob -Keep -ErrorAction SilentlyContinue
  2.         if ($progress)
  3.         {
  4.             $copiedfiles = ($progress | Select-String -SimpleMatch 'new file', 'newer')
  5.             if ($copiedfiles.count -le 0) { $TotalFilesCopied = $copiedfiles.Count }
  6.             else { $TotalFilesCopied = $copiedfiles.Count - 1 }
  7.             $FilesRemaining = ($totalfiles.count - $TotalFilesCopied)
  8.             $Bytesarray = @()
  9.             foreach ($Newfile in $copiedfiles)
  10.             {
  11.                 if ($Newfile.tostring().substring(13, 13).trim().length -eq 9) { $Bytesarray += $Newfile.tostring().substring(13, 15).trim() }
  12.                 else { $Bytesarray += $Newfile.tostring().substring(13, 13).trim() }
  13.             }
  14.             $bytescopied = ([int64]$Bytesarray[-1] * ($Filepercentcomplete/100))
  15.             $totalfilebytes = [int64]$Bytesarray[-1]
  16.             $TotalBytesCopied = ((($Bytesarray | Measure-Object -Sum).sum) - $totalfilebytes) + $bytescopied
  17.             $TotalBytesRemaining = ($totalbytes - $totalBytesCopied)
  18.             if ($copiedfiles)
  19.             {
  20.                 if ($copiedfiles[-1].tostring().substring(13, 13).trim().length -eq 9) { $currentfile = $copiedfiles[-1].tostring().substring(28).trim() }
  21.                 else { $currentfile = $copiedfiles[-1].tostring().substring(25).trim() }
  22.             }
  23.             $totalfilescount = $totalfiles.count
  24.             if ($progress[-1] -match '%') { $Filepercentcomplete = $progress[-1].substring(0, 3).trim() }
  25.             else { $Filepercentcomplete = 0 }
  26.             $totalPercentcomplete = (($TotalBytesCopied/$totalbytes) * 100)
  27.             if ($totalbytes -gt 2gb) { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied/1gb); $totalbytesprogress = "{0:N2}" -f ($totalbytes/1gb); $bytes = 'Gbytes' }
  28.             else { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied/1mb); $totalbytesprogress = "{0:N2}" -f ($totalbytes/1mb); $bytes = 'Mbytes' }
  29.             if ($totalfilebytes -gt 1gb) { $totalfilebytes = "{0:N2}" -f ($totalfilebytes/1gb); $bytescopied = "{0:N2}" -f ($bytescopied/1gb); $filebytes = 'Gbytes' }
  30.             else { $totalfilebytes = "{0:N2}" -f ($totalfilebytes/1mb); $bytescopied = "{0:N2}" -f ($bytescopied/1mb); $filebytes = 'Mbytes' }
  31.            
  32.             If ($totalPercentcomplete -gt 0)
  33.             {
  34.                 $TotalProgressbar.Value = $totalPercentcomplete
  35.             }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ProgressbarOverlay problems

Post by jvierra »

The Window Form cannot be updated while a command is running. Use a job or the Job Tracker custom control to allow a form to remain responsive while long running commands are executing.

A simple partial fix is to add a "DoEvents" command after each PowerCli command.

[System.Windows.Forms.Application]::DoEvents()
This topic is 6 years and 4 weeks 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