I'm trying to create a task scheduler that runs twice a day. I've implemented a task scheduler using CacheItemRemovedCallaback, as suggested in this post and this blog. I have the a feature that enables the admin to modified the scheduled times, and saves them on Application variables:
protected void UpdateSchedule(object sender, EventArgs e)
{
Button button = sender as Button;
if (button.ID == "scheduleButton1")
{
Application.Lock();
Application["buildSchedule1"] = GetScheduleTime1;
Application.UnLock();
}
else if (button.ID == "scheduleButton2")
{
Application.Lock();
Application["buildSchedule2"] = GetScheduleTime2;
Application.UnLock();
}
HttpRuntime.Cache.Remove(Global.DummyCachekey); //remove current scheduled task and set new time
}
And on Global.aspx I have:
protected void Application_Start(object sender, EventArgs e)
{
Application.Lock();
Application["buildSchedule1"] = new TimeSpan(10,00, 0); //default time
Application["buildSchedule2"] = new TimeSpan(16,00, 0); //default time
Application.UnLock();
SheduleTask();
};
The problem is that for some reason (probably due to app pool recycling) the schedule times get reset, and even some times the task won't start at the default times.
I found solutions that mention Windows services or Windows task scheduler, but that doesn't work for me since I need to be able to let the admin configure the times through the web application. (I search for this, but couldn't find anything).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…