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.
  1. Instantiate a new System.Windows.Forms.Timer component:
    Controls Region
    #region Controls
    $FormMain = New-Object -TypeName System.Windows.Forms.Form
    $GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox
    $ButtonRun = New-Object -TypeName System.Windows.Forms.Button
    $ButtonClose = New-Object -TypeName System.Windows.Forms.Button
    $StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip
    $ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel
    <#
    Instantiate component objects here...
    #>
    $TimerMain = New-Object -TypeName System.Windows.Forms.Timer
    #endregion
  2. Assign properties and event handler to the Timer component and add the $FormMain_Load handler to the main Form:
    Forms Region | $ShowMainForm
    #region Forms
    $ShowFormMain =
    {
    $FormWidth = 330
    $FormHeight = 260
    $FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
    $FormMain.Text = "Title"
    $FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    $FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)
    $FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
    $FormMain.MaximizeBox = $false
    $FormMain.AcceptButton = $ButtonRun
    $FormMain.CancelButton = $ButtonClose
    $FormMain.Add_Shown($FormMain_Shown)
    $FormMain.Add_Load($FormMain_Load)
    $GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)
    $GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))
    $FormMain.Controls.Add($GroupBoxMain)
    $ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))
    $ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonRun.TabIndex = 100
    $ButtonRun.Enabled = $false
    $ButtonRun.Text = "Run"
    $ButtonRun.Add_Click($ButtonRun_Click)
    $FormMain.Controls.Add($ButtonRun)
    $ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))
    $ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonClose.TabIndex = 101
    $ButtonClose.Text = "Close"
    $ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $FormMain.Controls.Add($ButtonClose)
    $StatusStripMain.SizingGrip = $false
    $StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    [void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)
    $FormMain.Controls.Add($StatusStripMain)
    <#
    Assign component properties and event handlers here...
    #>
    $TimerMain.Interval = 10000
    TimerMain.Add_Tick($TimerMain_Tick)
    [void]$FormMain.ShowDialog()
    $FormMain.Dispose()
    }
    #endregion
  3. Create the event handlers for $FormMain_Load and $TimerMain_Tick. This allows the Timer to start when the main Form is loaded and sets the action(s) to take when it reaches specific intervals:
    Handlers Region | $FormMain_Load
    #region Handlers
    $FormMain_Shown =
    {
    $ToolStripStatusLabelMain.Text = "Ready"
    $StatusStripMain.Update()
    $FormMain.Activate()
    }
    <#
    Add event handlers here...
    #>
    $FormMain_Load =
    {
    $TimerMain.Start()
    }
    $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()
    }
    }
    $ButtonRun_Click =
    {
    Invoke-FormAction -Action {
    try
    {
    <#
    Do work here...
    #>
    }
    catch
    {
    <#
    Add custom exception handling here...
    #>
    [void][System.Windows.Forms.MessageBox]::Show(
    $PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.",
    "Exception",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Warning
    )
    }
    } -Reset {
    <#
    Reset controls here...
    #>
    }
    }
    #endregion

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