如何使用Powershell使用GUI处理计时器?

时间:2019-06-21 01:51:31

标签: powershell user-interface

我有cmd脚本。我想使用计时器10秒钟来决定继续执行cmd脚本过程还是暂停它。 我想将此脚本放在cmd脚本的第一行

powershell.exe -ExecutionPolicy Bypass -File %~dp0\Pause_GUI.ps1

它将倒计时10秒,在10秒后,它将按返回错误级别继续执行cmd脚本过程,但是如果单击按钮暂停,则cmd脚本也将按返回错误级别暂停。 任何人都可以帮忙

已更新

#------------------------------------------- Add in Forms Controls -------------------------------------------#
Add-Type -AssemblyName System.Windows.Forms
#-------------------------------------------------------------------------------------------------------------#


#---------------------------------------- Begins creation of the form ----------------------------------------#
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "Message"
$MainForm.Width = 500
$MainForm.Height = 200
$MainForm.StartPosition = "CenterScreen"
$MainForm.BackColor = "#e2e2e2"
#-------------------------------------------------------------------------------------------------------------#


#----------------------------------------------- Button Clicks -----------------------------------------------#
$Auto_Button = ({ $global:result=1
                  $MainForm.Close() })
$Manual_Button = ({ $global:result=0
                    $MainForm.Close() })
#-------------------------------------------------------------------------------------------------------------#

#-------------------------------------------------- Buttons --------------------------------------------------#
$Automatic = New-Object System.Windows.Forms.Button
$Automatic.Location = New-Object System.Drawing.Size(110,80)
$Automatic.Size = New-Object System.Drawing.Size(120,30)
$Automatic.Text = "Continue After 10s"
$Automatic.BackColor = "#e47104"
$Automatic.Add_Click($Auto_Button)
$MainForm.Controls.Add($Automatic)

$Manual = New-Object System.Windows.Forms.Button
$Manual.Location = New-Object System.Drawing.Size(270,80)
$Manual.Size = New-Object System.Drawing.Size(100,30)
$Manual.Text = "Pause"
$Manual.BackColor = "#e47104"
$Manual.Add_Click($Manual_Button)
$MainForm.Controls.Add($Manual)
#-------------------------------------------------------------------------------------------------------------#

#--------------------------------------------- Displays the Form ---------------------------------------------#
$result=0
$MainForm.ShowDialog()
exit $result
#-------------------------------------------------------------------------------------------------------------#

如何将“ 10s之后继续”按钮用作计时器?并且GUI将在10秒后自动关闭

1 个答案:

答案 0 :(得分:1)

您需要一个System.Windows.Forms.Timer对象来计算您的时间,并需要一个.tick事件来触发您的时间。但是,您需要停止(处置)计时器,否则即使关闭窗口,计时器也将继续触发事件。 (在Powershell ISE中,这可能会导致您在加载窗口后立即关闭它们)。要从自己的事件中获取计时器,您需要在正确的范围内使用它。我为此使用了global范围。

$Auto_Button = ({ 
                    $global:Counter = 0
                    $global:timer = New-Object -type System.Windows.Forms.Timer
                    $global:timer.Interval = 1000
                    $global:timer.add_Tick({
                        if ($Counter -eq 10){
                            write-host $global:counter
                            $global:timer.Stop()
                            $global:timer.Dispose()
                            $result=1
                            $MainForm.Close()                    
                            $global:Counter++
                        }else{
                            write-host $global:counter
                            $global:Counter++
                        }
                    })
                    $global:timer.Start()
               })