asp.net 定时执行任务代码 定时采集数据
2024-10-21 06:43:27
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();//得到回写的字节流 }
}
}
最新文章
- Oracle 的字符集与乱码
- java多线程系类:JUC线程池:01之线程池架构
- RabbitMQ安装后不能运行 Error: unable to connect to node nodedown
- Qunie问题
- ————————————————————————————杭电ACM————————————————X-POWER————————————————————————————————
- 典当行以及海尔java小节
- decimal to hexadecimal,binary and octonary.
- 如何往IE工具条添加按钮(转载)
- (Problem 19)Counting Sundays
- Struts2--模块包含
- Redis讲解以及测试要点
- Atom插件下载失败解决办法
- 2017.07.09【NOIP提高组】模拟赛B组
- [转]Go语言中的make和new
- String拼接
- T4学习- 2、创建设计时模板
- 图片上传前预览、压缩、转blob、转formData等操作
- 浏览器缓存机制介绍 + 常用 http 状态码
- 异构GoldenGate 12c 双向复制配置
- UEFI BIOS和普通BIOS的区别
热门文章
- 分享一段ios数据库代码,包括对表的创建、升级、增删查改
- WebApp分析建模的工具
- 从Oracle迁移到MySQL的各种坑及自救方案
- [Web 前端] this作用域问题
- Orchard之在前台显式一个属于自己的列表
- glyphicons-halflings-regular.woff2:1 Failed to load resource: the server responded with a status of 404 (Not Found)解决Web部署 svg/woff/woff2字体 404错误
- 以双斜杠//开头的URL的含义
- the network bridge on device vmnet0 is not running解决方法
- 读书笔记,《Java 8实战》第五章,使用流
- vue-自定义组件传值