User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive
 

Timer Control [System.Windows.Forms.Timer]

Creates a timer that raises an event at intervals that you specify. Use the Timer event when you need to run code repeatedly at a specified interval. For example, you can use a Timer control to check the status of a process once every second.

Timer Control

Important Properties

Important Methods

Important Events


Important Properties: 

Enable Property

This property determines whether the timer is running.

Values (Default: False)

True

Indicates the timer is running or set to True to start the timer.

 

False

Indicates the timer is stopped or set to False to stop the timer.

 

Interval

This property represents the time, in milliseconds, before the Tick event is raised relative to the last occurrence of the Tick event.

Why use the Interval property?

Use the Interval property to change the default amount of time the Tick is event is triggered. If you want a slower timer, then increase the Interval amount, otherwise decrease the amount for a faster time. Note the Interval is in milliseconds; therefore, a value of 1000 = 1 second.

Default Value: 100 milliseconds

 

Important Methods:

Start

This method starts the timer. This method is equivalent to setting the Enable property to True.

$timer1.Start()

Stop

This method stops the timer. This method is equivalent to setting the Enable property to False.

$timer1.Stop()

 

Important Events:

Tick

This event occurs when the specified timer interval has elapsed and the timer is enabled.

Note: If another event such as a button click is processing a slow script, the Timer’s Tick event will not fire until after that event has completed processing.

Warning: If the amount of time to process a Tick event takes longer then the Interval time, it can lead to an overlap issue. Please be sure to leave an adequate amount of time for each Tick event to process. An alternate solution to this potential issue is to stop the timer while processing the Tick Event:

$timer1_Tick={
    #Temporarily stop the timer 
    $timer1.Stop()
    
    #Process the Tick Event Here...
    
    #Resume the timer
    $timer1.Start()
}

Warning: Timers can cause out of memory errors in long running scripts. Using the timer’s Stop method can help free allocated memory and thus prevent memory errors.

In the following example, we create a 60 second count down timer that utilizes the timer’s Tick event to update the display (label) with the remaining time.

$TotalTime = 60 #in seconds

$buttonStart_Click={
    $buttonStart.Enabled = $false
    #Add TotalTime to current time
    $script:StartTime = (Get-Date).AddSeconds($TotalTime)
    #Start the timer
    $timerUpdate.Start()
}

$timerUpdate_Tick={
    #Use Get-Date for Time Accuracy
    [TimeSpan]$span = $script:StartTime - (Get-Date)
    
    #Update the display
    $formSampleTimer.Text = $labelTime.Text = "{0:N0}" -f $span.TotalSeconds
    
    if($span.TotalSeconds -le 0)
    {
        $timerUpdate.Stop()
    }
}

The form displaying the remaining time:

Remaing Time

 

Download the Sample Timer.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.
Copyright © 2024 SAPIEN Technologies, Inc.