Timer Object in F#

Introduction

This article explains the Timer object and the properties of the Timer class in Windows Forms applications.

Timer Object

The Timer object allows users to set the time interval periodically to execute an event at a specified interval. The Timer object executes an event after a specifed interval. A Timer object raises an event at a given interval of time without using a secondary thread. The Timer class is a sealed class, in other words this class cannot be inherited.

Properties

The properties of Timer Objects are:

  • Timer.AutoReset: AutoReset indicates that the timer should raise the elapsed event continuously after the specified interval each time
  • Timer.Enabled: The Timer raises the elapsed event; you must set this to true if the timer is to do anything.
  • Timer.Interval: The number of milliseconds between elapsed events being raised. The default interval time is 100 milliseconds.
  • Timer.Start: Start raising the event by setting Enabled to true.
  • Timer.Stop: Stop raising the event by setting Enabled to false.
  • Timer.Close: Releases the resources used by the Timer.

Methods

The methods of the Timer Objects are:

  • Dispose(): Releases all resources used by the current instance of the Timer object.
  • ToString(): Returns a string that represents a current instance.
  • GetType(): Gets the type of the current instance.
  • Dispose(WaitHandle):  Releases all resources used by the current instance of the timer and signals when the timer has been disposed of.
  • Change(Int32 , Int32): Changes the start time and the interval between method invocations for a time.

Tick Event

The Tick Event handles when an action is performs during each postback and when the number of milliseconds specified in the "Interval" property of the timer class then the "Tick" event is raised.

Substring

Substring is the part of the string class that returns a new string that is a substring of the string. The substring begins at the specified given index and extended up to the given length. for example I have used Substring(10), in other words the number of characters in the substring.

Now  I will show you how to use Timer object in a Windows Forms application. Let's use the following procedure.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

CreatApplication

Step 2:

Now go the Solution Explorer on the right side of the application. Right-click on "References" and select "Add references".

SelectReferences

 

AddRefrences

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and "System.Drawing" while holding down the Ctrl key and click on "Ok."

ImortNamespaces

Step 4:

Write the following code Timer object records in the F# editor.

open System  

open System.Drawing   

open System.Windows.Forms    

let gettimeform=new Form(Text="Show Current Time")  

gettimeform.BackColor<-Color.DarkGray

let lblmsg=new Label(Top=60,Left=60,Width=130,BorderStyle=BorderStyle.FixedSingle)

lblmsg.Text<-"Current Time of System"

gettimeform.Controls.Add(lblmsg)

let lbltime=new Label(Top=100,Left=60,Width=115,BorderStyle=BorderStyle.FixedSingle)  

let ffont=new Font("Arial", 14.5F) 

let exitbutton=new Button(Top=140,Left=80)   

exitbutton.Text<-"Exit"

exitbutton.BackColor<-Color.Ivory

let timeobject=new Timer(Interval=1000,Enabled=true)  

lbltime.Font<-ffont  

timeobject.Tick.Add(fun showtime->  

let dtobject=Convert.ToString(System.DateTime.Now)  

let timepart=dtobject.Substring(10)  

lbltime.Text<-timepart)  

gettimeform.Controls.Add(lbltime)  

gettimeform.Controls.Add(exitbutton)  

exitbutton.Click.Add(fun _->  

timeobject.Stop()   

gettimeform.Close())                     

gettimeform.Show()  

Application.Run(gettimeform)  

Step 5 :

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:

AferDebug

Summary

This article explained the Timer object and the properties of the Timer object and methods of the Timer object then we explained Tick events and the Substring method in Windows Forms application.