I took help from :http://forum.codecall.net/topic/65434-c-working-with-timers/ (in it a decremented counter is used, but it's not working in my app)
I have some text field and two buttons: submit and update. I have implemented a timer from toolbar to update button.
I wanted this timer to run for 10 minutes and then disable the update button. But presently it's running for just 2 minutes.
Buttons Code:
<asp:Button ID="Btnsave" runat="server" CssClass="bt3dbuttons"
onclick="Btnsave_Click" OnClientClick="return confirm('Data Submitted')"
Text="Submit" Width="77px" />
<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick">
</asp:Timer>
<asp:Button ID="Butnupdate" runat="server" CssClass="btupbuttons"
onclick="Btnupdate_Click" Text="Update" visible="false" Width="85px" />
Here is the code for timer:
private System.Timers.Timer aTimer = new System.Timers.Timer(600000)
{ AutoReset = false };
protected void Timer2_Tick(object sender, EventArgs e)
{
aTimer = new System.Timers.Timer(600000);
aTimer.Interval = 600000;
double counter = aTimer.Interval;
counter++;
if (counter >= 600000)
{
Butnupdate.Enabled = false;
MessageBox.Show("Time Up!");
}
}
Code for Update Button:
protected void Btnupdate_Click(object sender, EventArgs e)
{
string id = Id.Text.Trim();
string name = Name.Text;
string project = Project.Text;
string result = Total.Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
try
{
//lets check that the user typed the first number
if (Viva.Text.Length > 1)
{
VivaLabel.Text = "Please enter a valid number to add.";
return;
}
//lets check that the user typed the second number
else if (Presentation.Text.Length > 1)
{
PresentationLabel.Text = "Please enter a valid number to add.";
return;
}
else if (Confidence.Text.Length > 1)
{
ConfidenceLabel.Text = "Please enter a valid number to add.";
return;
}
else if (System.Text.Length > 1)
{
SystemLabel.Text = "Please enter a valid number to add.";
return;
}
//Now we have valid inputs
//Lets put them into integer values
int number1 = int.Parse(Viva.Text);
int number2 = int.Parse(Presentation.Text);
int number3 = int.Parse(Confidence.Text);
int number4 = int.Parse(System.Text);
//Now lets add the numbers
int total = number1 + number2 + number3 + number4;
//lets place it into the TextBox3
Total.Text = total.ToString();
// cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"UPDATE Result SET Name = @name, Project = @project, Result = @result WHERE ID = @id";
con.Open();
cmd.Parameters.AddWithValue("@id", Id.Text.ToString());
cmd.Parameters.AddWithValue("@name ", Name.Text.ToString());
cmd.Parameters.AddWithValue("@project ", Project.Text.ToString());
cmd.Parameters.AddWithValue("@result ", Total.Text.ToString());
cmd.ExecuteNonQuery();
}
catch (Exception ex1)
{
//Report error to user in the bottom Label
MessageBox.Show(ex1.Message);
}
}
}
Problem has been solved by following code:
Create the timer ( default can be used from toolbox) set the timer interval property
For reference see: http://msdn.microsoft.com/en-us/library/system.web.ui.timer.interval.aspx
Create the event handler for timer :
use the following in the button on which you want your timer to start:
If you want to show the current system time or current time use
I declared it into label and how to do it and above please have a look here: http://msdn.microsoft.com/en-us/library/dd492144.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
Here we go a timer to disable button after 10 minutes :) Hopes it helps somebody as well :)
Use this code as a guide
Example taken from
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
Set the Interval Property of the asp:Timer this will then postback the page and call the ontick handler
http://msdn.microsoft.com/en-us/library/system.web.ui.timer.interval.aspx
I'm not sure what you are trying to do with the
System.Timers.Timer
on the server code it wont get used unless you page processing takes an extraordinary long time.According to the System.Timers.Timer class documentation, it fires up an event once the the defined time is elapsed, in an asynchronous way.
The setup code would look like this:
And the event itself like this:
Hope it works for you.
Your timer may be getting garbage collected if it is a long-running method instead of as part of the class, I cannot tell by the code posted.
Read the Timer.Interval Property documentation, which also has a sample of how to implement a timer.