【问题标题】:GUI-button to exit loop, Move-Item Job退出循环的 GUI 按钮,移动项目作业
【发布时间】:2019-09-25 08:44:02
【问题描述】:

当按下“关闭”按钮时,我正在努力打破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()

【问题讨论】:

  • 您在代码中的什么位置定义了$source$destination
  • 进一步。它不会让我在这里粘贴整个代码。

标签: powershell loops user-interface button break


【解决方案1】:

尽管有关于使用 DoEvents 的讨论,但您的按钮“关闭”点击似乎没有被触发这一事实与您正在循环中的 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()

附言您甚至在定义按钮之前就已将按钮添加到表单中,因此我将其删除。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多