PowerShell事件处理问题。在PowerShell中有事件监听器吗?

时间:2017-03-27 17:24:18

标签: powershell powershell-v3.0

请参阅以下代码。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$i=0; while($i -lt 10){write-host $i;start-sleep 2;$i++} })
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

在上面的代码中,我想要一个表单在单击确定按钮后在while循环进行时单击取消按钮单击。我搜索天堂,但找不到解决这个问题的办法。  换句话说,当单击ok按钮时,powershell会在循环时开始执行并冻结控件。但我想覆盖此控件,并允许在循环结束前单击取消按钮。可能吗?还有其他选择吗? 注意:我使用的是powershell 3.0

0 个答案:

没有答案