Timer
In a ScriptoForm, the Microsoft .NET Timer class is a component used to raise an Event at a specific interval. For example, you may need to check the status or availability of a network resource every 30 seconds and update the text value or other visual indicator of a control on the Form with that status value. Unlike other components, a Timer does not need to be added to the Form object - however, for consistency reasons, its properties are assigned in the
$ShowFormMain script block along with the other controls and components.- Instantiate a new
System.Windows.Forms.Timerobject in the Controls region. - Assign properties to the Timer in the
$ShowFormMainscript block in the Forms region. This should include the TimerIntervaland the name of theTickevent handler script block assigned to the Timer. - Create an Event Handler for the Timer to handle the
Tickevent actions that should happen at each interval. - Start the Timer in an appropriate event handler, such as when the user clicks a Button or the Form is loaded at the start of the script.
Examples
Section titled “Examples”# Create the Timer:$TimerMain = New-Object -TypeName System.Windows.Forms.Timer
# Set an inteval for the Timer in milliseconds and assign an event handler for the Tick event:$TimerMain.Interval = 10000$TimerMain.Add_Tick($TimerMain_Tick)
# Start the Timer when the main form is loaded:$FormMain.Add_Load($FormMain_Load)
$FormMain_Load ={ $TimerMain.Start()}
# Create an event handler for the "Tick" event of the Timer.$TimerMain_Tick = { $TimerMain.Stop() $ToolStripStatusLabelMain.Text = "Testing file server connection..." $StatusStripMain.Update() if (Test-Connection -ComputerName $FILE_SERVER -Quiet -Count 1) { $ToolStripStatusLabelMain.Text = "Ready" $StatusStripMain.Update() $ButtonRun.Enabled = $true } else { $ToolStripStatusLabelMain.Text = "Waiting for file server to come online..." $StatusStripMain.Update() $TimerMain.Start() }}The above example shows how to create a Timer component with a 10 second interval that starts when the Form is loaded.