asp.net 定时执行任务代码 定时采集数据

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.Net;
using System.IO;
using System.Text;
using System.Threading; namespace DTcms.Web
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
//定义定时器
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
myTimer.AutoReset = true;
} void myTimer_Elapsed(object source, ElapsedEventArgs e)
{
try
{
//Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ":AutoTask is Working!");
YourTask();
}
catch (Exception ee)
{
// Log.SaveException(ee);
}
} void YourTask()
{
if (DateTime.Now.ToString("HH:mm") == "15:20")
{
//在这里写你需要执行的任务
DTcms.BLL.article bll = new DTcms.BLL.article();
DTcms.Model.article_news model = new Model.article_news();
model.title = "定时任务 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
model.channel_id = 1;
model.category_id = 1;
bll.Add(model);
}
} protected void Application_End(object sender, EventArgs e)
{
// Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ":Application End!"); //下面的代码是关键,可解决IIS应用程序池自动回收的问题
Thread.Sleep(1000);
//这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Ap plication_Start
string url = "http://cpzy.net/e.aspx";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流 }
}
}
上一篇:shell 命令 zip unzip


下一篇:PAT A1020——已知后序中序遍历求层序遍历