Responsive Nested Loop

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 4 years and 9 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
escott0699
Posts: 25
Last visit: Thu Jan 12, 2023 7:52 am

Responsive Nested Loop

Post by escott0699 »

I am using the following article as a starting point. https://info.sapien.com/index.php/guis/ ... sive-loops
My code however, requires a foreach inside of another foreach similar to what is posted below. The $CancelLoop works as expected in "Loop 1" but does not work when processing the nested loop "Loop 2".
  1. #Variable that tells the loop to exit
  2. $script:CancelLoop = $false
  3.  
  4. $FormEvent_Load={
  5.     #TODO: Initialize Form Controls here
  6.     $buttonCancelLoop.Enabled = $false
  7. }
  8.  
  9. $buttonProcessLoop_Click={
  10.     #Init CancelLoop
  11.     $script:CancelLoop = $false
  12.     $buttonCancelLoop.Enabled = $true
  13.     #Disable the button so we don't trigger it again
  14.     $this.Enabled = $false
  15.     #Reset the Progress Bar
  16.     $progressbar1.Value = 0
  17.    
  18.     for ($x = 0;$x -lt 50; $x++) {
  19.         Write-Host "Loop 1"
  20.         Start-Sleep -Seconds 10
  21.         #process the pending message
  22.         [System.Windows.Forms.Application]::DoEvents()
  23.        
  24.         if ($script:CancelLoop -eq $true) {
  25.             #Clear the progress bar
  26.             $progressbar1.Value = 0
  27.             #Exit the loop
  28.             break;
  29.         }
  30.         for ($i = 0; $i -lt $progressbar1.Maximum; $i++) {
  31.             #Do work some work here
  32.             Write-Host "Loop 2"
  33.             Start-Sleep -Seconds 10
  34.            
  35.             #process the pending message
  36.             [System.Windows.Forms.Application]::DoEvents()
  37.            
  38.             if ($script:CancelLoop -eq $true) {
  39.                 #Clear the progress bar
  40.                 $progressbar1.Value = 0
  41.                 #Exit the loop
  42.                 break;
  43.             }
  44.             #Step the progress bar
  45.             $progressbar1.PerformStep()
  46.         }
  47.     }
  48.    
  49.    
  50.     #Enable the button so we can click it again
  51.     $this.Enabled = $true
  52.     $buttonCancelLoop.Enabled = $false
  53. }
  54.  
  55. $form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
  56. #Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
  57.     $script:CancelLoop = $true
  58. }
  59.  
  60. $buttonExit_Click={
  61.     #The Exit Button has DialogResult = Cancel
  62.     #So we don't have to call $form1.Close method
  63.     #But the Closing event will not trigger until
  64.     #the loop is finished processing
  65.     #So we just tell the loop to stop
  66.     $script:CancelLoop = $true
  67. }
  68.  
  69. $buttonCancelLoop_Click={
  70.     $script:CancelLoop = $true
  71. }
User avatar
escott0699
Posts: 25
Last visit: Thu Jan 12, 2023 7:52 am

Re: Responsive Nested Loop

Post by escott0699 »

I answered my own question. adding another variable $canceled in "Loop 2" that gets evaluated at the start of "Loop 1" prevents the first loop from processing. Maybe there is a better command like exit in the inner loop to avoid that?
  1. #Variable that tells the loop to exit
  2. $script:CancelLoop = $false
  3.  
  4. $FormEvent_Load={
  5.     #TODO: Initialize Form Controls here
  6.     $buttonCancelLoop.Enabled = $false
  7. }
  8.  
  9. $buttonProcessLoop_Click={
  10.     #Init CancelLoop
  11.     $script:CancelLoop = $false
  12.     $buttonCancelLoop.Enabled = $true
  13.     #Disable the button so we don't trigger it again
  14.     $this.Enabled = $false
  15.     #Reset the Progress Bar
  16.     $progressbar1.Value = 0
  17.    
  18.     for ($x = 0;$x -lt 50; $x++) {
  19.         If ($Canceled) {
  20.             Break
  21.         }
  22.         Write-Host "Loop 1"
  23.         Start-Sleep -Seconds 10
  24.         #process the pending message
  25.         [System.Windows.Forms.Application]::DoEvents()
  26.        
  27.         if ($script:CancelLoop -eq $true) {
  28.             #Clear the progress bar
  29.             $progressbar1.Value = 0
  30.             #Exit the loop
  31.             break;
  32.         }
  33.         for ($i = 0; $i -lt $progressbar1.Maximum; $i++) {
  34.             #Do work some work here
  35.             Write-Host "Loop 2"
  36.             Start-Sleep -Seconds 10
  37.            
  38.             #process the pending message
  39.             [System.Windows.Forms.Application]::DoEvents()
  40.            
  41.             if ($script:CancelLoop -eq $true) {
  42.                 #Clear the progress bar
  43.                 $progressbar1.Value = 0
  44.                 Write-Host "Loop 2 Stopped"
  45.                 #Exit the loop
  46.                 $Canceled = $true
  47.                 Break;
  48.             }
  49.             #Step the progress bar
  50.             $progressbar1.PerformStep()
  51.         }
  52.     }
  53.    
  54.    
  55.     #Enable the button so we can click it again
  56.     $this.Enabled = $true
  57.     $buttonCancelLoop.Enabled = $false
  58. }
  59.  
  60. $form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
  61. #Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
  62.     $script:CancelLoop = $true
  63. }
  64.  
  65. $buttonExit_Click={
  66.     #The Exit Button has DialogResult = Cancel
  67.     #So we don't have to call $form1.Close method
  68.     #But the Closing event will not trigger until
  69.     #the loop is finished processing
  70.     #So we just tell the loop to stop
  71.     $script:CancelLoop = $true
  72. }
  73.  
  74. $buttonCancelLoop_Click={
  75.     $script:CancelLoop = $true
  76. }
This topic is 4 years and 9 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