PowerShell中带有计时器的进度条

t1qtbnec  于 2022-11-10  发布在  Shell
关注(0)|答案(2)|浏览(221)

我正尝试在PowerShell中创建进度条,我希望进度条在一定时间后达到100%。下面的脚本可以做到这一点,但我必须单击按钮才能启动它。有没有办法不需要点击按钮就可以立即启动进度条?仅供参考:我只是想创建一个进度条,在设定的时间后将达到100%。以下是我的剧本:

  1. Function StartProgressBar
  2. {
  3. if($i -le 5){
  4. $pbrTest.Value = $i
  5. $script:i += 1
  6. }
  7. else {
  8. $timer.enabled = $false
  9. }
  10. }
  11. $Form = New-Object System.Windows.Forms.Form
  12. $Form.width = 400
  13. $Form.height = 200
  14. $Form.Text = "Add Resource"
  15. # Init ProgressBar
  16. $pbrTest = New-Object System.Windows.Forms.ProgressBar
  17. $pbrTest.Maximum = 100
  18. $pbrTest.Minimum = 0
  19. $pbrTest.Location = new-object System.Drawing.Size(10,10)
  20. $pbrTest.size = new-object System.Drawing.Size(100,50)
  21. $i = 0
  22. $Form.Controls.Add($pbrTest)
  23. # Button
  24. $btnConfirm = new-object System.Windows.Forms.Button
  25. $btnConfirm.Location = new-object System.Drawing.Size(120,10)
  26. $btnConfirm.Size = new-object System.Drawing.Size(100,30)
  27. $btnConfirm.Text = "Start Progress"
  28. $Form.Controls.Add($btnConfirm)
  29. $timer = New-Object System.Windows.Forms.Timer
  30. $timer.Interval = 1000
  31. $timer.add_Tick({
  32. StartProgressBar
  33. })
  34. $timer.Enabled = $true
  35. $timer.Start()
  36. # Button Click Event to Run ProgressBar
  37. $btnConfirm.Add_Click({
  38. While ($i -le 100) {
  39. $pbrTest.Value = $i
  40. Start-Sleep -m 1
  41. "VALLUE EQ"
  42. $i
  43. $i += 1
  44. }
  45. })
  46. # Show Form
  47. $Form.Add_Shown({$Form.Activate()})
  48. $Form.ShowDialog() ```
aoyhnmkz

aoyhnmkz1#

我对您的代码做了一些调整。

  • 我已将您的函数StartProgressBar重命名为Update-ProgressBar,因为它将在每个计时器节拍上被调用以更新状态栏,所以我认为这个名称更不言自明
  • 该按钮现在仅用于重新-在需要时启动进度条。
  • 您不需要计数器$i,因为ProgressBar对象本身具有跟踪其当前值所需的所有属性。此外,如果您确实需要在函数(和其他脚本块)中使用计数器,则需要使用$script:i对其进行寻址。没有该脚本作用域,$i就不存在。
  • 要使进度条在启动时开始运行,您需要使用$Form.Add_Shown({$timer.Enabled = $true; $timer.Start()})启用和启动计时器
  • 要在其他窗口顶部显示该窗体,请设置$Form.Topmost = $true

代码如下:

  1. function Update-ProgressBar {
  2. # this function will be called on every timer tick
  3. $pbrTest.PerformStep()
  4. $progressLabel.Text = "VALUE EQ $($pbrTest.Value)%"
  5. if ($pbrTest.Value -ge $pbrTest.Maximum) {
  6. $timer.Enabled = $false
  7. $timer.Stop()
  8. $btnRestart.Enabled = $true
  9. }
  10. }
  11. $Form = New-Object System.Windows.Forms.Form
  12. $Form.Width = 400
  13. $Form.Height = 200
  14. $Form.Text = "Add Resource"
  15. $Form.Topmost = $true # place this form on top
  16. # Init ProgressBar
  17. $pbrTest = New-Object System.Windows.Forms.ProgressBar
  18. $pbrTest.Location = New-Object System.Drawing.Point(10,10)
  19. $pbrTest.Size = New-Object System.Drawing.Size(100,50)
  20. $pbrTest.Maximum = 100
  21. $pbrTest.Minimum = 0
  22. $pbrTest.Value = 0
  23. $pbrTest.Step = 1
  24. $Form.Controls.Add($pbrTest)
  25. # Button
  26. $btnRestart = New-Object System.Windows.Forms.Button
  27. $btnRestart.Location = New-Object System.Drawing.Size(120,10)
  28. $btnRestart.Size = New-Object System.Drawing.Size(100,30)
  29. $btnRestart.Text = "Restart Progress"
  30. $btnRestart.Enabled = $false # disabled at first
  31. $btnRestart.Add_Click({
  32. # disable this button while the progressbar is running
  33. $this.Enabled = $false
  34. # reset the progressbar to value 0 and start the timer
  35. $pbrTest.Value = 0
  36. $timer.Enabled = $true
  37. $timer.Start()
  38. })
  39. $Form.Controls.Add($btnRestart)
  40. # Label
  41. $progressLabel = New-Object System.Windows.Forms.Label
  42. $progressLabel.Size = '370, 20'
  43. $progressLabel.Location = '10,70'
  44. $progressLabel.Text = "Processing..."
  45. $Form.Controls.Add($progressLabel)
  46. # Timer
  47. $timer = New-Object System.Windows.Forms.Timer
  48. $timer.Interval = 50
  49. $timer.Enabled = $false # disabled at first
  50. $timer.Add_Tick({ Update-ProgressBar })
  51. # start the timer as soon as the form is shown
  52. $Form.Add_Shown({$timer.Enabled = $true; $timer.Start()})
  53. # Show Form
  54. $Form.ShowDialog()
  55. # stop the timer and dispose of it and finally also dispose of the form
  56. $timer.Stop()
  57. $timer.Dispose()
  58. $pbrTest.Dispose()
  59. $Form.Dispose()
展开查看全部
vnzz0bqm

vnzz0bqm2#

我认为如果您删除onClick事件,它将自动运行,但我不知道这是否明显。

  1. Function StartProgressBar
  2. {
  3. if($i -le 5){
  4. $pbrTest.Value = $i
  5. $script:i += 1
  6. }
  7. else {
  8. $timer.enabled = $false
  9. }
  10. }
  11. $Form = New-Object System.Windows.Forms.Form
  12. $Form.width = 400
  13. $Form.height = 200
  14. $Form.Text = "Add Resource"
  15. # Init ProgressBar
  16. $pbrTest = New-Object System.Windows.Forms.ProgressBar
  17. $pbrTest.Maximum = 100
  18. $pbrTest.Minimum = 0
  19. $pbrTest.Location = new-object System.Drawing.Size(10,10)
  20. $pbrTest.size = new-object System.Drawing.Size(100,50)
  21. $i = 0
  22. $Form.Controls.Add($pbrTest)
  23. # Button
  24. $btnConfirm = new-object System.Windows.Forms.Button
  25. $btnConfirm.Location = new-object System.Drawing.Size(120,10)
  26. $btnConfirm.Size = new-object System.Drawing.Size(100,30)
  27. $btnConfirm.Text = "Start Progress"
  28. $Form.Controls.Add($btnConfirm)
  29. $timer = New-Object System.Windows.Forms.Timer
  30. $timer.Interval = 1000
  31. $timer.add_Tick({
  32. StartProgressBar
  33. })
  34. $timer.Enabled = $true
  35. $timer.Start()
  36. # Button Click Event to Run ProgressBar
  37. While ($i -le 100) {
  38. $pbrTest.Value = $i
  39. Start-Sleep -m 1
  40. "VALLUE EQ"
  41. $i
  42. $i += 1
  43. }
  44. # Show Form
  45. $Form.Add_Shown({$Form.Activate()})
  46. $Form.ShowDialog()
展开查看全部

相关问题