GUI progress info for running tasks

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 2 years and 11 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
cjtechie
Posts: 3
Last visit: Mon Apr 12, 2021 5:06 pm

GUI progress info for running tasks

Post by cjtechie »

I am working to create a GUI to run a number of computer configuration tasks. I have successfully configured the form and commands. The issue I am running into at this point is that some of the tasks take some time to complete. I am trying to enact some action that will inform the user that the tasks are running. At this point, clicking on the Complete Actions button will start the tasks and grey out the button to advise the user that it is running. I am struggling to add a visual cue for the user based on the running tasks. I am not picky on its implementation. Whether it be the button changing back to an actionable state, a progress bar of some sort, or output showing the completion of tasks. Any guidance or assistance on this would be greatly appreciated.
progress.txt
(2.19 KiB) Downloaded 110 times
cjtechie
Posts: 3
Last visit: Mon Apr 12, 2021 5:06 pm

Re: GUI progress info for running tasks

Post by cjtechie »

  1. # Some dummy jobs for illustration purposes
  2. Start-Job -ScriptBlock{Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 0 } -Name RemoteDesktop
  3. Start-Job -ScriptBlock{Set-TimeZone -Id "US Mountain Standard Time" } -Name MountainTime
  4. Start-Job -ScriptBlock{powercfg /change monitor-timeout-ac 10 | powercfg /change monitor-timeout-dc 10 | powercfg /change standby-timeout-ac 0 | powercfg /change standby-timeout-dc 15 } -Name PowerSettings
  5. Start-Job -ScriptBlock{gpupdate /force } -Name GroupPolicy
  6. Start-Job -ScriptBlock{Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000113}" } -Name SCCMUpdate
  7.  
  8.  
  9. # Get all the running jobs
  10. $jobs = get-job | ? { $_.state -eq "running" }
  11. $total = $jobs.count
  12. $runningjobs = $jobs.count
  13.  
  14. # Loop while there are running jobs
  15. while($runningjobs -gt 0) {
  16.     # Update progress based on how many jobs are done yet.
  17.     write-progress -activity "Events" -status "Progress:" `
  18.    -percentcomplete (($total-$runningjobs)/$total*100)
  19.  
  20.     # After updating the progress bar, get current job count
  21.     $runningjobs = (get-job | ? { $_.state -eq "running" }).Count
  22. }
cjtechie
Posts: 3
Last visit: Mon Apr 12, 2021 5:06 pm

Re: GUI progress info for running tasks

Post by cjtechie »

I found the above code that works independently in the ISE but I am unsure how to integrate it into my existing form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI progress info for running tasks

Post by jvierra »

To add in any code in a form you have to place the code in an event like a button click.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI progress info for running tasks

Post by jvierra »

To run asynchronous tasks consider the following article:
https://info.sapien.com/index.php/guis/ ... sive-forms
This topic is 2 years and 11 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