0

I have a form that runs a Progress Bar. I need to wait 32 minutes and so I was using the ProgressBar on my GUI form to display continued progress along with messages. I run through the Do/While, execute a 1 minute sleep, increment, and repeat until the variable increments to 32 (32 minutes therefore having passed). However, the form seems to freeze and lock up, unable to be moved or minimized, while the 1 minute sleep runs (so basically for 32 minutes)...

I've tried Start-Job and Wait-Job, but have a feeling I'm not utilizing it properly. I don't quite understand how to effectively take the 1 minute sleep and process it as a side-job on a different thread from a script standpoint.

The function that processes the loop & sleep:

function WaitSync {
   #Removes all of the old controls
   foreach ($control in $aControls2) {$MainForm.Controls.Remove($control)}

   #Adds the progress bar and message
   $MainForm.Controls.Add($pbSync)
   $MainForm.Controls.Add($lPhrases)

   #Initializes the increment variable
   $z = 0

   while ($z -le 33) {
      $lPhrases.Text = "Updated Text..."
      $pbSync.Value = $z
      $pbSync.Refresh()
      Start-Sleep -Seconds 60
      $z++
   }

   #Adds the Complete Button to close the Form
   $MainForm.Controls.Add($bComplete)
}

How do I make it so the form can be at least minimized while the progress bar processes (the 32 minutes wait out)?

Matthew N.
  • 23
  • 4
  • Is this winforms? – Enigmativity Aug 27 '19 at 23:41
  • .NET forms through PowerShell. – Matthew N. Aug 28 '19 at 01:02
  • 2
    Do not use `Start-Sleep` in GUIs but use events instead. Possible duplicate of [Start-Sleep pauses entire function](https://stackoverflow.com/questions/54028379/start-sleep-pauses-entire-function) – iRon Aug 28 '19 at 02:18
  • @MatthewN. - What is ".NET forms"? Do you mean winforms? – Enigmativity Aug 28 '19 at 03:34
  • @iRon thank you - I don't quite understand what exactly the Delay-Command is doing, but I can definitely understand how to implement and use it. I will give that a try tomorrow. – Matthew N. Aug 28 '19 at 03:47
  • 1
    More specific to your case: build a timer that fires an event every 60 second. Create a global counter (or use a timer property) that increase on that event and stop/kill the timer when it reaches 33 (bonus features: you might think of creating e.g. a `Cancel` button that stops the timer immediately on a click event). – iRon Aug 28 '19 at 10:55
  • @iRon I will do some reading on Form Timers. I'm struggling to accomplish the above recommendation to my case as I still don't quite understand how Timers/Events work and how to manipulate them. Let me know if you have any recommended readings for this particular topic. – Matthew N. Aug 28 '19 at 23:22
  • I found https://devblogs.microsoft.com/scripting/use-asynchronous-event-handling-in-powershell/ but it's for System Timers not Form Timers - not sure if that matters. – Matthew N. Aug 28 '19 at 23:22

0 Answers0