I have a method which must be run every 5minutes in order to show latest data in gridview.
I was experimenting with System.Timers.Timer and the following is my code:
protected void Page_Load(object sender, EventArgs e)
{
try
{
lblError.Text = "";
t = new System.Timers.Timer(300000);//every 5min = 300000
t.Enabled = true;
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
if (!Page.IsPostBack)
{
//t = new System.Timers.Timer(300000);//every 5min = 300000
//t.Enabled = true;
//t.Elapsed += new ElapsedEventHandler(t_Elapsed);
floor = ddFloors.SelectedValue.ToString();
GenerateStatus();
}
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
//Response.Redirect("Home.aspx");
floor = ddFloors.SelectedValue.ToString();
GenerateStatus();
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
the problem is that after 5minutes it is not going to t_Elapsed. Note this should keep being done at all times not just for once. any help pls?
You need to better understand the client/server model of web applications. By the time this event fires the page has long since been rendered and sent to the browser.
If you want to do something like this you will have to do it on the client using javascript. A combination of a jQuery plugin like jQuery timers and using an AJAX call back to the server should help you achieve what you want.