Powershell如何停止进度条

时间:2018-11-16 23:18:46

标签: powershell

我正在尝试从数组中读取数据并显示进度条。
我的目标是停止循环和进度条,并以此方式完成操作。

function ButtonStart_Click {

     $LabelCounter.visible = $true
     $ProgressBar1.Visible = $true
     $ButtonStart.Enabled = $false
     $ButtonStop.Enabled = $true
     $script:CancelLoop = $false

     # =============================================================
     # LOOP EXCEL DATA OBJECT
     # =============================================================
     $Counter = 0
     $ErrorCounter = 0;

     foreach ($Row in $ExcelData) {

        if($script:CancelLoop -eq $true){
           $progressbar1.Value = 0
           break;
        }

        $Counter++ 
        [Int]$Percentage = ($Counter/$ExcelData.Count)*100 
        $ProgressBar1.Value = $Percentage 
        $LabelCounter.Text = "Show $Counter of " +$ExcelData.Count
        [System.Windows.Forms.Application]::DoEvents() 
        Start-Sleep -Milliseconds 900

     }

     $ButtonStop.Enabled = $false
}

function ButtonStop_Click {
    $ProgressBar1.Value = 0
    $script:CancelLoop = $true
}

不幸的是,它不起作用,因为在循环开始后,我尝试单击“停止”按钮(即使已启用),直到循环结束也没有任何反应。
过去,我使用do-until来开始-停止进度条,但没有读取数组,效果很好。

function ButtonStart_Click {

    $LabelCounter.visible = $true
    $ProgressBar1.Visible = $true
    $TextBoxSubject.ReadOnly = $true

    $Progressbar1.Maximum = 100

    $script:CancelLoop = $false
    $ButtonStop.Enabled = $true
    $this.Enabled = $false
    $Progressbar1.Value = 0

    do{

       if($script:CancelLoop -eq $true){
          $progressbar1.Value = 0
          break;
       }
       $Progressbar1.PerformStep()
       [System.Windows.Forms.Application]::DoEvents()
       sleep -Milliseconds 900

    }
    until($Progressbar1.Value -eq $Progressbar1.Maximum)

    $this.Enabled = $true
    $ButtonStop.Enabled = $false

 }

我不明白我哪里错了。
我该怎么办?
谢谢

0 个答案:

没有答案