我正在尝试在我的消息框中显示一个计时器,这是我用PS表单创建的。我想要这样的东西:
“你的电脑将在10秒内关机”,在1秒后。
“您的电脑将在9秒后关机”
“你的电脑将在8秒内关机”等等。
希望你能帮助我。
发布于 2016-08-11 02:26:22
我看不到刷新消息框中文本的方法。如果我必须这样做,我可能会弹出另一个带有标签的表单,并使用一个计时器来刷新每个刻度上的标签文本。
下面是一个使用潜在起点的代码示例:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form = New-Object system.Windows.Forms.Form
$script:Label = New-Object System.Windows.Forms.Label
$script:Label.AutoSize = $true
$script:Form.Controls.Add($Label)
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$script:CountDown = 60
$Timer.add_Tick(
{
$script:Label.Text = "Your system will reboot in $CountDown seconds"
$script:CountDown--
}
)
$script:Timer.Start()
$script:Form.ShowDialog()
您将需要扩展以满足您的需求,例如,当倒计时到0时,条件逻辑可以执行任何您想要的操作(例如,重新启动),也许可以添加一个中止按钮,等等。
发布于 2016-08-11 00:09:59
有来自Windows Script Host的PopUp Method,它可以让你为PopUp
设置一个time to live
。我不认为有办法不刷新来自PowerShell的消息框(不要引用我的话)。代码中有两行来自here。
不确定这是否是你想要的,但这是可行的(粗糙的解决方案):
$timer = New-Object System.Timers.Timer
$timer.AutoReset = $true #resets itself
$timer.Interval = 1000 #ms
$initial_time = Get-Date
$end_time = $initial_time.AddSeconds(12) ## don't know why, but it needs 2 more seconds to show right
# create windows script host
$wshell = New-Object -ComObject Wscript.Shell
# add end_time variable so it's accessible from within the job
$wshell | Add-Member -MemberType NoteProperty -Name endTime -Value $end_time
Register-ObjectEvent -SourceIdentifier "PopUp Timer" -InputObject $timer -EventName Elapsed -Action {
$endTime = [DateTime]$event.MessageData.endTime
$time_left = $endTime.Subtract((Get-Date)).Seconds
if($time_left -le 0){
$timer.Stop()
Stop-Job -Name * -ErrorAction SilentlyContinue
Remove-Job -Name * -ErrorAction SilentlyContinue
#other code
# logoff user?
}
$event.MessageData.Popup("Your PC will be shutdown in $time_left sec",1,"Message Box Title",64)
} -MessageData $wshell
$timer.Start()
编辑:@JonDechiro提出的解决方案比我的干净得多,也更适合OP的要求。
https://stackoverflow.com/questions/38851482
复制相似问题