【ASP.NET 进阶】定时执行任务实现 (定时读取和修改txt文件数字内容,无刷新显示结果)
现在有很多网站或系统需要在服务端定时做某件事情,如每天早上8点半清理数据库中的无效数据等等,Demo 具体实现步骤如下:
0.先看解决方案截图
1.创建ASP.NET项目TimedTask,然后新建一个全局应用程序类文件 Global.asax
2.然后在Application_Start 事件中 启动定时器,如需要每隔多少秒来做一件事情,即在后台执行,与客户端无关,即使客户端全部都关闭,那么后台仍然执行,具体代码如下:
- 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 TimedTask
- {
- public class Global : System.Web.HttpApplication
- {
- private void AddCount(object sender, ElapsedEventArgs e)
- {
- //在这里编写需要定时执行的逻辑代码
- FileControl.ChangeFileNumber();
- }
- protected void Application_Start(object sender, EventArgs e)
- {
- System.Timers.Timer timer = new System.Timers.Timer();
- //AddCount是一个方法,此方法就是每个1秒而做的事情
- timer.Elapsed += new System.Timers.ElapsedEventHandler(AddCount);
- timer.Interval = ;// 设置引发时间的时间间隔,此处设置为1秒
- timer.Enabled = true;
- timer.AutoReset = true;
- }
- 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)
- {
- // 在应用程序关闭时运行的代码
- //下面的代码是关键,可解决IIS应用程序池自动回收的问题
- //局限性:可以解决应用程序池自动或者手动回收,但是无法解决IIS重启或者web服务器重启的问题,当然这种情况出现的时候不多,而且如果有人访问你的网站的时候,又会自动激活计划任务了。
- Thread.Sleep();
- //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start
- string url = "http://www.cnblogs.com/yc-755909659/";
- HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
- HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
- Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
- }
- /*原理:Global.asax 可以是asp.net中应用程序或会话事件处理程序,我们用到了Application_Start(应用程序开始事件)和Application_End(应用程序结束事件)。
- * 当应用程序开始时,启动一个定时器,用来定时执行任务AddCount()方法,这个方法里面可以写上需要调用的逻辑代码,可以是单线程和多线程。
- * 当应用程序结束时,如IIS的应用程序池回收,让asp.net去访问当前的这个web地址。这里需要访问一个aspx页面,这样就可以重新激活应用程序。*/
- }
- }
Global.asax
3.简单的循环事件实现:读取txt文件中的数字,实现每秒递加
(1) 先新建 testfile.txt 文件,里面为数字1。
(2) 读取和修改txt文件实现:
- public class FileControl
- {
- private const string testFilePath = "~/testfile.txt";
- public static string GetFileNumber()
- {
- //获得物理路径
- string filePath = System.Web.Hosting.HostingEnvironment.MapPath(testFilePath); string text = System.IO.File.ReadAllText(filePath);
- return text;
- }
- public static string ChangeFileNumber()
- {
- string text = GetFileNumber();
- string newText = (int.Parse(text) + ) + ""; //数字增加1
- string filePath = System.Web.Hosting.HostingEnvironment.MapPath(testFilePath);
- System.IO.File.WriteAllText(filePath, newText, System.Text.Encoding.UTF8);
- return text;
- }
- }
4.测试页面利用官方控件无刷新实现文件数字显示
(1) Html代码
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Test</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input id="txtValue" type="text" />
- <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <ContentTemplate>
- <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
- <asp:Label ID="lb_Value" runat="server" Text="1"></asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
- </div>
- </form>
- </body>
- </html>
(2)cs 代码
- namespace TimedTask
- {
- public partial class TestForm : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- TimeStart();
- }
- }
- protected void Timer1_Tick(object sender, EventArgs e)
- {
- TimeStart();
- }
- private void TimeStart()
- {
- lb_Value.Text = "Runtime:" + FileControl.GetFileNumber() + " s";
- }
- }
- }
实现效果:
源代码:TimedTask.zip
【ASP.NET 进阶】定时执行任务实现 (定时读取和修改txt文件数字内容,无刷新显示结果)的更多相关文章
- JAVA定时执行任务,每天定时几点钟执行任务
JAVA定时执行任务,每天定时几点钟执行任务的示例如下: 1.建立TimerManage类,设置时间点,时间点设置的管理类,代码如下: package com.pcitc.time; import j ...
- Asp.net读取和写入txt文件方法(实例)!
Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...
- Ubuntu定时执行任务(定时爬取数据)
cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效. 1:编辑crontab文件,用来存放你要执行的命令 s ...
- asp.net 定时执行任务代码 定时采集数据
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- SPring中quartz的配置(可以用实现邮件定时发送,任务定时执行,网站定时更新等)
http://www.cnblogs.com/kay/archive/2007/11/02/947372.html 邮件或任务多次发送或执行的问题: 1.<property name=" ...
- osql执行数据库查询命令并保存到txt文件
osql -Usa -P123 -d AppBox -Q "select * from Menus where sortindex > 1000" -o e:\xxx.txt ...
- linux下使用crontab定时执行脚本
使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...
- 最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件
今天笔者就要归纳总结下一整套测试流程,从无到有,实现零突破,包括如何编写测试用例,定时执行测试用例,查找最新生成的测试报告文件,自动发送最新测试报告邮件,一整套完整的测试流程.以后各位只要着重如何编写 ...
- windows上定时执行php文件
<?php $fp = fopen("E:/wwwroot/test/plan.txt", "w+"); fwrite($fp, date("Y ...
随机推荐
- 35 Gallery – Ajax Slide
http://html5up.net/overflow (PC,Mobile,Table) http://bridge.net/ https://github.com/bridgedotnet/Bri ...
- ActiveReports 9 新功能:创新的报表分层设计理念
在最新发布的ActiveReports 9报表控件中添加了多项新功能,以帮助你在更短的时间里创建外观绚丽.功能强大的报表系统,本文将重点介绍创新的报表分层设计理念,对报表内容进行分组管理与设计,易于实 ...
- Convert string to binary and binary to string in C#
String to binary method: public static string StringToBinary(string data) { StringBuilder sb = new S ...
- AngularJS directive 指令相关记录
.... .directive('scopeDemo',function(){ return{ template: "<div class='panel-body'>Name: ...
- Mvc项目架构分享之项目扩展
Mvc项目架构分享之项目扩展 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构各部分解析 5.项目 ...
- win10应用部署到手机出现问题Exception from HRESULT: 0x80073CFD
今天把应用部署到手机上时,出现了这样的问题 Exception from HRESULT: 0x80073CFD 具体错误是: Error Error : DEP0001 : Unexpected E ...
- 参加:白帽子活动-赠三星(SAMSUNG) PRO....
参加:白帽子活动-—赠三星(SAMSUNG) PRO.... Everybody~小i在这里提前祝大家国庆假期愉快,咱们期待已久的国庆活动终于开始拉,下面进入正题,恩,很正的题! 活动地址:http: ...
- 参加2013中国大数据技术大会(BDTC2013)
2013年12月5日-6日参加了为期两天的2013中国大数据技术大会(Big Data Technology Conference, BDTC2013),本期会议主题是:“应用驱动的架构与技术 ”.大 ...
- C语言可变参数函数实现原理
一.可变参数函数实现原理 C函数调用的栈结构: 可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈. 本 ...
- 深入了解Activity-生命周期
一 介绍 Activity是android中使用最为频繁的组件,在官方文档中是这样描述的:An activity is a single, focused thing that the user ca ...