Powershell - 一段时间后关闭表单 [英] Powershell - Close form after time period

查看:101
本文介绍了Powershell - 一段时间后关闭表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 power-shell 表单,提示用户是否愿意推迟关闭时间(关闭时间由另一个 ps 脚本作为参数给出,该参数在初始脚本中计算).

I have a power-shell form prompting user if they would like to postpone a shutdown time (the time of the shutdown is given by another ps script as an argument which is calculated in the initial script).

我想在 30 分钟不活动后关闭表单,我该如何处理?

I'd like to close the form after 30 minutes of inactivity, how would I go about this?

Powershell 代码(表单):

Powershell Code (Form):

#creating the form object

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


#setting the from title
$form.Text = "System warning"

#setting the form dimesnions and positions 
$form.Size = New-Object System.Drawing.Size(300,250) 
$form.StartPosition = "CenterScreen"


#claculate time before shut down:
$StartDate=(GET-DATE)
#this ill have to be replaced by a parameter which indicates the shut down date
$EndDate = $EndDate | Get-Date 
$timeDifference = NEW-TIMESPAN –Start $StartDate –End $EndDate
$secondsTilShutdown = $timeDifference.TotalSeconds


#time remaining message
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,40)  
$label.Text = "This computer is scheduled to shutdown at " + $EndDate 
$form.Controls.Add($label) 


#second message 
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,70) 
$label.Size = New-Object System.Drawing.Size(280,20)  
$label.Text = "Postpone the shutdown by : " 
$form.Controls.Add($label) 


#setting up the drop down box (time in minutes)
$TimePeriods = 60, 120, 180, 240  

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,90)
$DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($time in $TimePeriods) {
      [void] $DropDown.Items.Add([string]($time/60) + " hours")
    }
#1 hour is the default  
$DropDown.SelectedItem = $DropDown.Items[0]    
$Form.Controls.Add($DropDown)



#creating the postpone button  
$OKButton = New-Object System.Windows.Forms.Button
#position of the button on the form
$OKButton.Location = New-Object System.Drawing.Point(25,130)
#size of the button
$OKButton.Size = New-Object System.Drawing.Size(100,50)
#text of the button
$OKButton.Text = "Postpone"
#the value that the from dialog will return if we click on ok
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
#assigns a key down on the enter key to the button
$form.AcceptButton = $OKButton
#adds the button to the form
$form.Controls.Add($OKButton)

#creating the ignore button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(175,130)
$CancelButton.Size = New-Object System.Drawing.Size(100,50)
$CancelButton.Text = "Continue with scheduled shutdown"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)


$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()


###processing form results : 


#if the postpone button button was selected 
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    #logic
}

elseif($result -eq [System.Windows.Forms.DialogResult]::Cancel)
{
   #we do not push back the shut down time 
}

推荐答案

使用定时器,我给你举个例子来关闭定时器

use a timer, i give you a example to timer wich close form

 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()

这篇关于Powershell - 一段时间后关闭表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆