Reputation: 377
I had inserted a countdown timer in my project. When I run it , the timer start but when I stop debugging it and after some time when I again run it then what I notice is, the timer is still running. For example, I have given time of 3 minutes to the timer i.e. after 3 minutes it should redirect to the result page...now I run the page and the count down timer is started...when I stop debugging it suppose at 00:02:45(time left) and after 5 seconds when I again run it then the timer continues with 00:02:40(time left) . I don't want like this to be happen . How do I stop the continuation of the timer? Have a look at my code. Show me where I am making mistake and what is the solution.
On the TimerTest.aspx.cs page, I have the following lines of code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Student_TimerTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["end_t"] == null)
{
DateTime start_time = DateTime.Now;
DateTime end_time = start_time.AddMinutes(3);
Session["end_t"] = end_time;
}
}
protected void timer1_tick(object sender, EventArgs e)
{
DateTime dt = (DateTime)Session["end_t"];
DateTime dt_curr = DateTime.Now;
TimeSpan ts = dt - dt_curr;
lblTimer.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
if (ts.Minutes == 0)
{
timer1.Enabled = false;
Response.Redirect("/Student/Result.aspx");
}
}
}
On the TimerTest.aspx page, I have inserted the following lines of code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TimerTest.aspx.cs" Inherits="Student_TimerTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>
</div>
<div>
<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimer" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 2350
Reputation: 418
To say that the timer is still running is a little misleading. There isn't actually any code running after you quit debugging. However, you store the start time in session state which, by default, is stored in the browser. If you stop debugging and then start again, the application instance for your web app is restarted, but your browser preserves the session state. So your browser still has the old start time. Therefore, on page load, the line if (Session["end_t"] == null)
results in false. There is a value there.
There are many options here, but one would be to always clear the value on page load if it's not a post back. If it is a post back, just check if there's a value in session already.
Sample code:
protected void Page_Load(object sender, EventArgs e)
{
// If this is the initial page load (not a post back),
// or if there's not already an end time in Session,
// then set the end time
if (!Page.IsPostBack || Session["end_t"] == null)
{
DateTime start_time = DateTime.Now;
DateTime end_time = start_time.AddMinutes(3);
Session["end_t"] = end_time;
}
}
Upvotes: 1