如何在 Powershell 消息框中获取计时器? [英] How to get a timer in a Powershell messagebox?
问题描述
我正在尝试在我用 PS Forms 创建的消息框中显示一个计时器.我想要这样的东西:
I'm trying to display a timer in my message box that I created with PS Forms. I would like something like that:
您的 PC 将在 10 秒后关闭"1 秒后.
" Your PC will be shutdown in 10 sec" after 1 sec.
您的电脑将在 9 秒后关闭"
"Your PC will be shutdown in 9 sec"
您的电脑将在 8 秒后关闭"等.
"Your PC will be shutdown in 8 sec" and so on.
希望你能帮助我.
推荐答案
我没有看到刷新消息框中文本的方法.如果我必须这样做,我可能会弹出另一个带有标签的表单,并使用一个计时器来刷新每个刻度上的标签文本.
I don't see a way of refreshing the text in a message box. If I had to do this I would probably pop up another form with a label and use a timer that refreshes the text of the label on each tick.
这是一个使用潜在起点的代码示例:
Here is a code example to use a potential starting point:
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 时执行您想做的任何操作(例如,重新启动),或者添加一个用于中止的按钮等.
You will need to expand to meet your needs such as conditional logic to do whatever action you want (e.g., reboot) when the count down reaches 0, perhaps add a button for aborting, etc.
这篇关于如何在 Powershell 消息框中获取计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!