【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 />");
            }
        }
    }
}
复制代码

转载于:https://www.cnblogs.com/tsql/p/6649794.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值