Winforms Powershell-时间段后关闭窗体

Winforms Powershell-时间段后关闭窗体,winforms,powershell,timer,Winforms,Powershell,Timer,我有一个powershell表单,提示用户是否希望推迟关机时间(关机时间由另一个ps脚本作为参数给出,该参数在初始脚本中计算) 我想在30分钟不活动后关闭表单,我该怎么做 Powershell代码(表格): 使用计时器,我给你一个例子来说明计时器的关闭形式 Function ClearAndClose() { $Timer.Stop(); $Form.Close(); $Form.Dispose(); $Timer.Dispose(); $Scri

我有一个powershell表单,提示用户是否希望推迟关机时间(关机时间由另一个ps脚本作为参数给出,该参数在初始脚本中计算)

我想在30分钟不活动后关闭表单,我该怎么做

Powershell代码(表格):


使用计时器,我给你一个例子来说明计时器的关闭形式

 Function ClearAndClose()
 {
    $Timer.Stop(); 
    $Form.Close(); 
    $Form.Dispose();
    $Timer.Dispose();
    $Script:CountDown=5
 }

 Function Button_Click()
 {
    ClearAndClose
 }

 Function Timer_Tick()
 {

    $Label.Text = "Your system will reboot in $Script:CountDown seconds"
         --$Script:CountDown
         if ($Script:CountDown -lt 0)
         {
            ClearAndClose
         }
 }



 Add-Type -AssemblyName System.Windows.Forms
 Add-Type -AssemblyName System.Drawing
 $Form = New-Object system.Windows.Forms.Form
 $Form.Text = "Attention redémarrage!!"
 $Form.Size = New-Object System.Drawing.Size(250,100)
 $Form.StartPosition = "CenterScreen"
 $Form.Topmost = $True


 $Label = New-Object System.Windows.Forms.Label
 $Label.AutoSize = $true
 $Label.Location = New-Object System.Drawing.Size(20,5)


 $Button = New-Object System.Windows.Forms.Button
 $Button.Location = New-Object System.Drawing.Size(55,35)
 $Button.Size = New-Object System.Drawing.Size(120,23)
 $Button.Text = "STOP"
 $Button.DialogResult=[System.Windows.Forms.DialogResult]::OK

 $Timer = New-Object System.Windows.Forms.Timer
 $Timer.Interval = 1000

 $Form.Controls.Add($Label)
 $Form.Controls.Add($Button)

 $Script:CountDown = 6

 $Button.Add_Click({Button_Click})
 $Timer.Add_Tick({ Timer_Tick})


 $Timer.Start()
 $Form.ShowDialog()

您可以启动计时器,在窗体启动时关闭窗体。这很有帮助,但请注意
$form.Dispose()
足以关闭和处理窗体(隐式处理其所有控件)和
$timer;Dispose()
足以停止并处理计时器-无需额外清理。是:)我让窗体最终关闭事件OnClose OnClose:)
 Function ClearAndClose()
 {
    $Timer.Stop(); 
    $Form.Close(); 
    $Form.Dispose();
    $Timer.Dispose();
    $Script:CountDown=5
 }

 Function Button_Click()
 {
    ClearAndClose
 }

 Function Timer_Tick()
 {

    $Label.Text = "Your system will reboot in $Script:CountDown seconds"
         --$Script:CountDown
         if ($Script:CountDown -lt 0)
         {
            ClearAndClose
         }
 }



 Add-Type -AssemblyName System.Windows.Forms
 Add-Type -AssemblyName System.Drawing
 $Form = New-Object system.Windows.Forms.Form
 $Form.Text = "Attention redémarrage!!"
 $Form.Size = New-Object System.Drawing.Size(250,100)
 $Form.StartPosition = "CenterScreen"
 $Form.Topmost = $True


 $Label = New-Object System.Windows.Forms.Label
 $Label.AutoSize = $true
 $Label.Location = New-Object System.Drawing.Size(20,5)


 $Button = New-Object System.Windows.Forms.Button
 $Button.Location = New-Object System.Drawing.Size(55,35)
 $Button.Size = New-Object System.Drawing.Size(120,23)
 $Button.Text = "STOP"
 $Button.DialogResult=[System.Windows.Forms.DialogResult]::OK

 $Timer = New-Object System.Windows.Forms.Timer
 $Timer.Interval = 1000

 $Form.Controls.Add($Label)
 $Form.Controls.Add($Button)

 $Script:CountDown = 6

 $Button.Add_Click({Button_Click})
 $Timer.Add_Tick({ Timer_Tick})


 $Timer.Start()
 $Form.ShowDialog()