用于退出循环的 GUI 按钮,Move-Item Job

GUI-button to exit loop, Move-Item Job

当按下 "Off" 按钮时,我正在努力打破 while 循环。 相反,在开始移动过程后,我 运行 陷入了无限循环。

$objForm.Controls.Add($StartButton)
$objForm.Controls.Add($OffButton)

$OffButton = New-Object System.Windows.Forms.Button
$OffButton.Location = New-Object System.Drawing.Size(340,105)
$OffButton.Size = New-Object System.Drawing.Size(90,28)
$OffButton.Text = 'Off'
$OffButton.Name = 'Off'
$OffButton.Font = [System.Drawing.Font]::New("Microsoft Sans Serif", 11)
$OffButton.Add_Click({$script:AktiveLoop = $False})
$objForm.Controls.Add($OffButton)

$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(340,135)
$StartButton.Size = New-Object System.Drawing.Size(90,28)
$StartButton.Text = 'Start'
$StartButton.Name = 'Start'
$StartButton.Font = [System.Drawing.Font]::New("Microsoft Sans Serif", 11)
$StartButton.Add_Click({
    $script:AktiveLoop = $true

    while ($script:AktiveLoop -eq $true) {
        Move-Item $source -Destination $destination
        Start-Sleep -Seconds 1.0

        if ($script:AktiveLoop = $False) {
            break
        }
    }
})
[void] $objForm.ShowDialog()

虽然有关于使用 DoEvents 的讨论,但您的按钮 'Off' 单击似乎没有被触发这一事实与您放入循环中的 Start-Sleep 有关.

Start-Sleep 使表单无响应。如果您使用 [System.Windows.Forms.Application]::DoEvents() 而不是处理按钮单击事件。

此外,从用户的角度来看,最好在首次绘制表单时禁用“关闭”按钮,并在 Click 事件中切换两个按钮的启用启动。

为此我对示例中给出的代码做了一些修改:

Add-Type -AssemblyName System.Windows.Forms

$objForm   = New-Object System.Windows.Forms.Form

$OffButton = New-Object System.Windows.Forms.Button
$OffButton.Location = New-Object System.Drawing.Size(340,105)
$OffButton.Size = New-Object System.Drawing.Size(90,28)
$OffButton.Text = 'Off'
$OffButton.Name = 'Off'
$OffButton.Font = [System.Drawing.Font]::New("Microsoft Sans Serif", 11)
# initially disable the 'Off' button
$OffButton.Enabled   = $false
$OffButton.Add_Click({
    $script:AktiveLoop   = $false
    $OffButton.Enabled   = $false
    $StartButton.Enabled = $true

})
$objForm.Controls.Add($OffButton)

$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(340,135)
$StartButton.Size = New-Object System.Drawing.Size(90,28)
$StartButton.Text = 'Start'
$StartButton.Name = 'Start'
$StartButton.Font = [System.Drawing.Font]::New("Microsoft Sans Serif", 11)
# initially enable the 'Off' button
$StartButton.Enabled = $true
$StartButton.Add_Click({
    # switch the buttons Enabled status so the user can not click the same button twice
    $OffButton.Enabled   = $true
    $StartButton.Enabled = $false
    $script:AktiveLoop   = $true

    while ($script:AktiveLoop) {
        # perform some action here. (added -WhatIf here for testing)
        Move-Item $source -Destination $destination -WhatIf

        # Start-Sleep makes the form unresponsive, so don't use that.

        # DoEvents() suspends the current thread and processes 
        # window messages like a click on the 'Off' button.
        [System.Windows.Forms.Application]::DoEvents()
    }
})
$objForm.Controls.Add($StartButton)


[void] $objForm.ShowDialog()
$objForm.Dispose()

p.s。您甚至在定义按钮之前就将它们添加到表单中,所以我将其删除。

希望对您有所帮助