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).
Using hacks like Windows Scheduled Tasks and Control Panel abilities are not nice solutions. They sucks most of the time, they are a headache.
You can use ATrigger scheduling service. A .Net library is also available to create scheduled tasks without overhead.
Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.
That's what you should be using. A web application doesn't "always run." It responds to requests, that's all. Unless something is actively making a request to the website, it's not doing anything.
Sure it does. More than one application can share a single database. In this case you'd have two applications: