【ASP.NET 进阶】定时执行任务

简介:

原理:利用全局应用程序类 Global.asax 和 System.Timers.Timer  类定时处理任务。

 

示例效果图:

其 Global.asax 类代码如下:

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Timers;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace TimingTask
{
    /**
     *原理:Global.asax 可以是asp.net中应用程序或会话事件处理程序,
     *我们用到了Application_Start(应用程序开始事件)和Application_End(应用程序结束事件)。
     *当应用程序开始时,启动一个定时器,用来定时执行任务YourTask()方法,
     *这个方法里面可以写上需要调用的逻辑代码,可以是单线程和多线程。
     *当应用程序结束时,如IIS的应用程序池回收,让asp.net去访问当前的这个web地址。
     *这里需要访问一个aspx页面,这样就可以重新激活应用程序。 
     */
    public class Global : System.Web.HttpApplication
    {
        //在应用程序启动时运行的代码  
        protected void Application_Start(object sender, EventArgs e)
        {
            //定时器
            System.Timers.Timer myTimer = new System.Timers.Timer(2000);
            myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
            myTimer.Enabled = true;
            myTimer.AutoReset = true;
        }
        private void myTimer_Elapsed(object source, ElapsedEventArgs e)
        {
            try
            {
                RunTheTask();
            }
            catch (Exception ex)
            {
                WebForm1.html = ex.Message;
            }
        }

        private void RunTheTask()
        {
            //在这里写你需要执行的任务
            WebForm1.html = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!";
        }

        // 在新会话启动时运行的代码  
        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }
        // 在出现未处理的错误时运行的代码  
        protected void Application_Error(object sender, EventArgs e)
        {

        }
        // 在会话结束时运行的代码。   
        protected void Session_End(object sender, EventArgs e)
        {
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为  
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer   
            // 或 SQLServer,则不会引发该事件
        }
        //  在应用程序关闭时运行的代码  
        protected void Application_End(object sender, EventArgs e)
        {
            WebForm1.html = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!";

            //下面的代码是关键,可解决IIS应用程序池自动回收的问题  

            Thread.Sleep(1000);

            //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start  
            string url = "WebForm1.aspx";
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流  
        }
    }
}
复制代码

 

然后用 WebForm 页面输出定时效果:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimingTask
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public static String html = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDataBind();
            }
        }

        private void GetDataBind()
        {
            for (int i = 0; i <10; i++)
            {
                System.Threading.Thread.Sleep(1000);
                Response.Write(html+"<br />");
            }
        }
    }
}





本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/4503669.html,如需转载请自行联系原作者

目录
相关文章
|
Web App开发 前端开发 .NET
基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度
在之前的文章《推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler》和《简单、轻量、功能非常强大的C#/ASP.NET定时调度任务执行管理组件–FluentScheduler之实例篇》中,我们认识和了解了FluentScheduler这款轻量的定时任务调度执行组件。
2220 0
|
开发框架 缓存 .NET
ASP.NET Core : 十一. 如何在后台运行一个任务
在大部分程序中一般都会需要用到后台任务, 比如定时更新缓存或更新某些状态。
180 0
|
存储 开发框架 缓存
ASP.NET Core : 十一. 如何在后台运行一个任务(上)
在大部分程序中一般都会需要用到后台任务, 比如定时更新缓存或更新某些状态。
158 0
|
Web App开发 .NET 测试技术
使用ASP.NET实现Windows Service定时执行任务
使用ASP.NET实现Windows Service定时执行任务 我们怎样才能在服务器上使用asp.NET定时执行任务而不需要安装windows service?我们经常需要运行一些维护性的任务或者像发送提醒邮件给用户这样的定时任务。
2510 0