Introduction to Timer in ASP.NET
ASP.NET provides a very important functionality called Timer. It is basically used to provide a specific interval of time in any application. Timer in ASP.NET control helps us in taking an action or performing a specific action after a certain time span in an application. You must have seen in many applications like if you are giving an exam on the website, it has a certain time limit and after that, a specific action will take place (example: the page gets submitted automatically). Therefore, in this case, the developer has used the Timer control functionality in his application. We can also create a timer in such a way that it can be resumed or restarted.
Events in Timer
Some Events in timer which are usually used and important.
- Tick: This is the most commonly used event that occurs when the interval is elapsed.
- Start: It will set the value by setting enabled to true when it starts raising the tick event.
- Stop: It will stop the tick event raised by setting the value to false.
- Close: It will Release the resources, which was used by the timer.
- Enabled: It is used to check whether the tick event has been raised by the Timer.
- Interval: It is the time span on which the event needs to be raised by the timer.
- AutoRest: It will indicate or check for the time interval the tick event is raised or not that is if we have provided 10 seconds then it will check the time interval of 10 seconds and looks that timer has triggered the tick event or not.
Examples of Timer in ASP.NET
Given below are the examples of Timer in ASP.NET:
Example of Timer Control. The below example will update a timestamp after every 10 seconds.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<title>Timer Control Example</title>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timerrunat="server" id="UpdateTimer" interval="10000" ontick="UpdateTimer_Tick" />
<asp:UpdatePanelrunat="server" id="TimedPanel" updatemode="Conditional">
<asp:AsyncPostBackTriggercontrolid="UpdateTimer" eventname="Tick" />
<asp:Labelrunat="server" id="DateStampLabel" />
Below is the Code Behind Function, which we need to add in our Code Behind File.
protected void UpdateTimer_Tick(object sender, EventArgs e)
DateStampLabel.Text = DateTime.Now.ToString();
Here we can see a normal UpdatePanel, which will carry a trigger reference to the new Timer Control. It is due to timer “ticks”, by which tick event is fired and the panel is updated. For setting the timer, the “interval” attribute is used which will define the number of milliseconds to appear before it fires the tick event.We can use another approach by including the Time inside the UpdatePanel. By this method, we don’t have to define a trigger. However, its behavior is different for the Timer inside the UpdatePanel and outside the Update Panel.
Timer is not re-constructed until the update panel is fully updated it happens in the case when a Timer is inside UpdatePanel. In a simple way if the timer has a time span of 60 seconds and it takes 5 seconds to update, then the next event will fire at 65 seconds, not the 60 seconds. On the other side, if timer is outside the UpdatePanel, it will fire at exactly 60 seconds and the user will see the content only for 55 seconds before its updated.
Example of the share market was in every second market change. In the below example w have added 2 display related web controls in the UpdatePanel.
- A label control named as “SharePrice”, which is used to display the price of the share market.
- CurrentPanelTime is another label control, which displays the last updated time of UpdatePanel.
<asp:ScriptManager ID="MyManager" runat="server">
Latest Time of "full" postback at:
<asp:Label ID="CurrentTime" runat="server"></asp:Label></p>
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
Current Share Market price:
<asp:Label ID="ShrarePrice" runat="server" Font-Bold="True"></asp:Label><br />
Last refreshed time of UpdatePanel:
<asp:Label ID="CurrentPanelTime" runat="server"></asp:Label>
<asp:Timer ID="MyTimer" runat="server" Interval="10000">
We have added a timer control to the UpdatePanel and set its “Interval” property to 10 seconds that is 1000 milliseconds. This will lead to a timer tick, which causes updating after every 10 seconds and will raise its tick event. UpdateSharePricemethod is called when the page is loaded very first and whenever the ‘tick’ event is raised of timer control. In the code behind class, create a Page_Load event handler and set two label text to the current time.
Now call theUpdateSharePrice Method if the Page. Is Postback is false which means the page is visited for the very first time and not on a postback?
protected void Page_Load(object sender, EventArgs e)
CurrentTime.Text = DateTime.Now.ToLongTimeString();
CurrentPanelTime.Text = DateTime.Now.ToLongTimeString();
After that, we need to create an event handler for the ‘Tick’ Timer event and from there we need to call the UpdateSharePricemethod.
protected void MyTimer_Tick(object sender, EventArgs e)
// Update the stock price
When we visit the page via a browser after every 10 seconds timer will cause a partial page postback. Now the current market share price is computed and it will be updated on the display.
We can see the below screenshot when the user visited very first time.
The portal has visited the very first time by the user, therefore, the latest time of “full” postback time and Refreshed time are the same. We can see the current share market price provided at 11:00:05 AM time. After 10 seconds, the page is updated. We can see the Latest time of “full” postback and refreshed time is different having 10 seconds of difference in them. With another 10 seconds, it will again update and fetch the Current Share Market Price.
Timer Control in ASP.NET framework helps in providing a trigger to postback every ‘n’ number of milliseconds.by using Timer control functionality we can build many more applications where time constraint matters to an application including bank application such as token generation for security purpose. Timer Control makes developers’ life easy.