Timer on Server Side?
I want to have a Timer to update 开发者_运维问答the global variable like every 10 sec, so I put a timer in my Global.asax.cs:
void Application_Start(object sender, EventArgs e)
{
Timer aTimer = new Timer();
aTimer.Interval = 10*1000;
aTimer.Tick += aTimer_Tick;
aTimer.Start();
}
void aTimer_Tick(object sender, EventArgs e)
{
// Update Data
}
But weird thing is nothing happen after 10 sec. I wonder if is possible to do it like that?
Thanks in advance.
Use System.Timers.Timer
instead of System.Windows.Forms.Timer
The former uses Elasped
as the event handler and will function as expected almost anywhere in an application. The latter is tailored for winforms and should be used on Form1_Load()
not on application start.
ASP.NET requests and events are not designed to take a long time. You should consider writing a Windows Service, depending on what you're trying to achieve (what are you trying to achieve?).
I guess that the Timer
object is destroyed after the Application_Start
void is closed. So, try to assign it to an application variable.
精彩评论