当前位置: 代码迷 >> ASP.NET >> 定时执行事件,该怎么处理
  详细解决方案

定时执行事件,该怎么处理

热度:1177   发布时间:2013-02-25 00:00:00.0
定时执行事件
程序中中,我想每天晚上执行一个回收数据的事件,请问怎么实现。
(定时执行回收数据的方法(比如23:00))

------解决方案--------------------------------------------------------
定时器
------解决方案--------------------------------------------------------
timer定时检测当前时间
或者用windows的计划任务
------解决方案--------------------------------------------------------
定时器或使用sql作业
------解决方案--------------------------------------------------------
如果回收数据的事件可以和程序分离,就用windows的任务计划。
如果无法分离,就用Quartz.net任务调度框架。
还可以自己搞定,用timer,定时检测时间,
------解决方案--------------------------------------------------------
启用SQL Server 代理,新建sql作业,定时每天23:00执行该作业(你要做的事情:一段数据库维护代码)
------解决方案--------------------------------------------------------
写个windows服务呗,类似的例子
------解决方案--------------------------------------------------------
http://blog.csdn.net/jinlong5200/article/details/3182451 这个比较好
------解决方案--------------------------------------------------------
定时任务 Windows Service
------解决方案--------------------------------------------------------
探讨

我的程序是oracle数据库

------解决方案--------------------------------------------------------
数据库里面的作业 

也可以在代码里面处理。。

Global里面定义一个timmer

在Application_start里面对timer进行处理
C# code
 System.Timers.Timer timer;  protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            RegisterGlobalFilters(GlobalFilters.Filters);            RegisterRoutes(RouteTable.Routes);            timer = new System.Timers.Timer(1000*60*5);//5分钟            timer.Elapsed += new System.Timers.ElapsedEventHandler(Auto_Run);            timer.Start();         }        void Auto_Run(object sender, System.Timers.ElapsedEventArgs e)        {            PerformanceController controller = new PerformanceController();            controller.AutoRun();                   }
------解决方案--------------------------------------------------------
这个写个windows服务比较好:
C# code
    public partial class MyServer : ServiceBase    {        public MyServer()        {            InitializeComponent();            this.ServiceName = "MyServer";        }        Thread th = null;        bool isStop = false;        protected override void OnStart(string[] args)        {            if (th == null)            {                th = new Thread(new ThreadStart(delegate                {                    while (!isStop)                    {                        //上午10点和下午4点                        if ((DateTime.Now.Hour == 10 || DateTime.Now.Hour == 16) && DateTime.Now.Minute == 0)                        {                           //执行你的任务                        }                        Thread.Sleep(60000);                    }                }));            }            th.Start();        }        protected override void OnStop()        {            isStop = true;            Thread.Sleep(3000);            if (th != null)            {                th.Abort();                th = null;            }        }        protected override void OnPause()        {        }        protected override void OnContinue()        {        }            }
------解决方案--------------------------------------------------------
Timer定时器,右下角托盘,再放入系统启动项。我当年有个程序就是这么做的,实际情况很不错。
  相关解决方案