Reputation: 129
I want to stop the timer after a predefined time. I am making an online examination application where student will not be able to continue with examination after predefined time. I have implemented the timer, it is working well but I want that timer should automatically get stopped after say 1 hr 20 mins. So my question is that how to write that condition and where? I am using ASP.net C#. My sample code is :
static DateTime dt;
int m_StartTime = Environment.TickCount;
long m_Interval = new TimeSpan(0, 1, 0).Ticks;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DateTime(2011, 1, 1, 0, 0, 0);
Timer1.Enabled = false;
Timer1.Interval = 1000;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
dt = dt.AddSeconds(10);
Label1.Text = dt.ToString("H:mm:ss");
if (Environment.TickCount - m_StartTime > m_Interval)
{
Timer1.Enabled=false;
}
}
protected void start_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
Upvotes: 2
Views: 11700
Reputation: 908
public partial class _Default : System.Web.UI.Page
{
static DateTime dt = new DateTime(2014,9,1,0,0,0,000);
Timer t = new Timer();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Timer1.Enabled = false;
Timer1.Interval = 1;
lbl.Text = dt.ToLongTimeString() + ":" + dt.Millisecond;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
dt = dt.AddMilliseconds(1);
lbl.Text = dt.ToLongTimeString() + ":" + dt.Millisecond;
}
protected void Reset_Click(object sender, EventArgs e)
{
Timer1.Enabled = false; //For stop the timer
dt = new DateTime(2014, 9, 1, 0, 0, 0, 000);
lbl.Text = dt.ToLongTimeString() + ":" + dt.Millisecond;
}
protected void Pause_Click(object sender, EventArgs e)
{
Timer1.Enabled = false; //For stop the timer
}
protected void Start_Click(object sender, EventArgs e)
{
Timer1.Enabled = true; // For starting the timer
}
}
Upvotes: 0
Reputation: 3443
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DateTime(2011, 1, 1, 0, 0, 0);
Timer1.Enabled = false;
Timer1.Interval = 1000;
Timer2.Enabled = false;
Timer2.Interval = 1000 * 60 * MIN_PER_TEST; // set interval for a test, if it is changing, do this in Button1_click [startstop=="stop"] before setting Enabled = true
startstop = "Stop";
}
}
// end of the test came, do what you need to do in order to kill the student mood further more :)
protected void Timer2_Tick(object sender, EventArgs e)
{
Button1_Click(sender, e); // simulate clicking STOP, though might want to replace that!
}
// Added start and stop for the test duration timer.
protected void Button1_Click(object sender, EventArgs e)
{
if (startstop == "Stop")
{
startstop = "Start";
Timer1.Enabled = true;
Timer2.Enabled = true;
Button1.Text = "Stop";
}
else
{
startstop = "Stop";
Timer1.Enabled = false;
Timer2.Enabled = false;
Button1.Text = "Start";
}
}
Hope this is helpful.
Upvotes: 0
Reputation: 30355
I think you can just store the number of ticks when you start the timer Environment.TickCount
and then in the Timer1_Tick
event you check if the current number ticks - stored number of ticks is greater than your interval. Then you stop the timer. You can get a value to compare to like that: new TimeSpan(1, 20, 0).Ticks
...
Timer1.Enabled = true;
m_StartTime = Environment.TickCount
m_Interval = new TimeSpan(1, 20, 0).Ticks;
...
protected void Timer1_Tick(object sender, EventArgs e)
{
dt = dt.AddSeconds(1);
Label1.Text = dt.ToString("H:mm:ss");
if (Environment.TickCount - m_StartTime > m_Interval)
{
Timer1.Stop();
}
}
You can of course make a constant and not a variable and so on, this is just an example to illustrate the idea.
P.S. by the way it seems you need just a boolean startstop instead of a string.
Upvotes: 2
Reputation: 191
In asp.net it isnt that easy to use timers i suggest you should read this msdn-article. For general i would recommend you to use a second timer for that purpose instead for adding extra logic to the first one. Also you should use an enum or bool for startstop.
Upvotes: 0