Skip to content

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.
  1. Instantiate a new System.Windows.Forms.Timer object in the Controls region.
  2. Assign properties to the Timer in the $ShowFormMain script block in the Forms region. This should include the Timer Interval and the name of the Tick event handler script block assigned to the Timer.
  3. Create an Event Handler for the Timer to handle the Tick event actions that should happen at each interval.
  4. 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.
PowerShell
# 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.

Timer Class (System.Windows.Forms) | Microsoft Learn