在ASP.NET计划任务 [英] Schedule task in ASP.NET

查看:137
本文介绍了在ASP.NET计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个每天运行两次任务调度。我实现了使用CacheItemRemovedCallaback任务调度程序,如这篇文章建议并的这个博客。我有一个功能,使管理员修改的预定时间,节省他们在Application变量:

 保护无效UpdateSchedule(对象发件人,EventArgs的发送)
{
    Button按钮=发送者的按钮;
    如果(button.ID ==scheduleButton1)
    {        Application.Lock();
        应用[buildSchedule1] = GetScheduleTime1;
        Application.UnLock();
    }
    否则,如果(button.ID ==scheduleButton2)
    {
        Application.Lock();
        应用[buildSchedule2] = GetScheduleTime2;
        Application.UnLock();
    }
    HttpRuntime.Cache.Remove(Global.DummyCachekey); //删除当前计划任务,并设置新的时间
 }

和上Global.aspx我有:

 保护无效的Application_Start(对象发件人,EventArgs的发送)
{
    Application.Lock();
    应用[buildSchedule1] =新的时间跨度(10,00,0); //默认时间
    应用[buildSchedule2] =新的时间跨度(16,00,0); //默认时间
    Application.UnLock();
    SheduleTask();
};

现在的问题是,由于某种原因(可能是由于应用程序池回收)的计划时间得到恢复,甚至一些次任务将不会在预设时间启动。

我发现,提及的Windows服务或Windows任务调度解决方案,但因为我需要能够让管理员通过Web应用程序配置的时代,这并不为我工作。 (我搜索了这一点,但找不到任何东西)。


解决方案

  

我发现,提及的Windows服务或Windows任务调度解决方案


这是你应该使用什么。 Web应用程序并不始终运行。它响应请求,仅此而已。除非采取积极做好该网站的请求时,它没有做任何事情。


  

但因为我需要能够让管理员通过Web应用程序

配置倍,这并不为我工作

当然它。多个应用程序可以共享一个数据库。在这种情况下,你有两个应用:


  • Web应用程序,用户可以登录并保持相关的计划任务中的数据。

  • 运行在服务器上的背景和执行配置的任务,它从数据库中读取Windows服务(或计划的控制台应用程序)。

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).

解决方案

I found solutions that mention Windows services or Windows task scheduler

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.

but that doesn't work for me since I need to be able to let the admin configure the times through the web application

Sure it does. More than one application can share a single database. In this case you'd have two applications:

  • A Web Application where users can login and maintain the data related to the scheduled tasks.
  • A Windows Service (or scheduled Console Application) which runs in the background on the server and executes the configured tasks which it reads from the database.

这篇关于在ASP.NET计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆