0

I'm trying to refresh the Label on the form every 10s, but it does not matter what I set the $timer.Interval to, it runs continuously. How can I set the interval?

function UpdateProcCount()
{
    $Label.Text = (Get-date).ToFileTime()

}

$Form = New-Object System.Windows.Forms.Form
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 10000
$timer.Add_Tick({UpdateProcCount})
$timer.Enabled = $True
$Form.Text = "Sample Form"
$Label = New-Object System.Windows.Forms.Label
$Label.Text = " "
$Label.AutoSize = $True
$Form.Controls.Add($Label)
$Form.ShowDialog()

Running on Win8,

> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14409  1012    

Edit

If I run it as a .ps1 it works every time, I can set the interval and it will work for any, but in ISE it works the first time, then if I close the form and run it again with other interval values it does not work. I tried even to remove the variables but somehow it gets set someplace...

RBA
  • 783
  • 8
  • 20
  • FYI: Works on my machine in ISE. Same PS version, but on Windows 7... so could be a bug with the Win8 version? – JohnLBevan Aug 21 '18 at 14:38
  • Ah actually; if I decrease the value it works; if I increase it it uses the previous value... I think because the previous timer still exists; so each time we run it we're adding another parallel thread with its own timer. – JohnLBevan Aug 21 '18 at 14:40
  • 1
    See https://stackoverflow.com/questions/44308958/the-powershell-ise-sometimes-behaves-unpredictably-after-code-changes-are-made/44309250 – JohnLBevan Aug 21 '18 at 14:43
  • 1
    yup, adding $timer.Dispose() makes it work on ISE! Thanks a lot! – RBA Aug 21 '18 at 15:29

1 Answers1

0

Answered by @JohnLBevan in the comments.

related question: The PowerShell ISE sometimes behaves unpredictably after code changes are made

adding $timer.Dispose() at the end makes it work on ISE.

RBA
  • 783
  • 8
  • 20