现在有很多网站或系统需要在服务端定时做某件事情,如每天早上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文件数字内容,无刷新显示结果)的更多相关文章

  1. JAVA定时执行任务,每天定时几点钟执行任务

    JAVA定时执行任务,每天定时几点钟执行任务的示例如下: 1.建立TimerManage类,设置时间点,时间点设置的管理类,代码如下: package com.pcitc.time; import j ...

  2. Asp.net读取和写入txt文件方法(实例)!

    Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...

  3. Ubuntu定时执行任务(定时爬取数据)

    cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效. 1:编辑crontab文件,用来存放你要执行的命令 s ...

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

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  5. SPring中quartz的配置(可以用实现邮件定时发送,任务定时执行,网站定时更新等)

    http://www.cnblogs.com/kay/archive/2007/11/02/947372.html 邮件或任务多次发送或执行的问题: 1.<property name=" ...

  6. osql执行数据库查询命令并保存到txt文件

    osql -Usa -P123 -d AppBox -Q "select * from Menus where sortindex > 1000" -o e:\xxx.txt ...

  7. linux下使用crontab定时执行脚本

    使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...

  8. 最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件

    今天笔者就要归纳总结下一整套测试流程,从无到有,实现零突破,包括如何编写测试用例,定时执行测试用例,查找最新生成的测试报告文件,自动发送最新测试报告邮件,一整套完整的测试流程.以后各位只要着重如何编写 ...

  9. windows上定时执行php文件

    <?php $fp = fopen("E:/wwwroot/test/plan.txt", "w+"); fwrite($fp, date("Y ...

随机推荐

  1. Oracle数据库导入导出总结(dmp文件)

    Oracle 10G 管理页面(Oracle Enterprise Manager 10g): http://localhost:1158/em http://localhost:1158/em/co ...

  2. javascript: detect mobile devices or browser

    http://detectmobilebrowsers.com/ http://hgoebl.github.io/mobile-detect.js/ http://www.hand-interacti ...

  3. HDU 2577---How to Type

    HDU  2577 Description Pirates have finished developing the typing software. He called Cathy to test ...

  4. (旧)子数涵数·C语言——让C帮你做计算

    之前,我们学过了我们的第一个C程序--hello World.现在开始进一步学习,想一想如何让C帮你做计算. 我们先来看代码(我没有新建,还是用之前的hello world.cpp): 好,因为之前在 ...

  5. 总结一下SQL的全局变量

    SQL Server 2008中的全局变量及其用法 T-SQL程序中的变量分为全局变量和局部变量两类,全局变量是由SQL Server系统定义和使用的变量.DBA和用户可以使用全局变量的值,但不能自己 ...

  6. java响应微信用户信息(wechat4j)

    你的微信应用程序接收到用户发送的消息事件之后,可以进行响应.wechat4j支持多种消息的响应. wechat4j响应消息的部分在wechat4j的入口WechatSupport.java中定义,直接 ...

  7. 基于UML项目的分析与设计

    1,概述 项目中需求和设计的文档是必然的,UML工具可以帮助指导我们从不同的角度去看待一个新的系统,并把这个系统分解剖析出来.本篇文章主要讲述的是如何将UML应用到项目的开发工作中,关于如何学习UML ...

  8. C语言位运算详解

    位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题.C语言提供了6个位操作运算符.这些运算符只能用于整形操作数,即只能用于带符号或无符号的char.short.int与long类型 ...

  9. atitit.木马病毒webshell的原理and设计 java c# .net php.

    atitit.木马病毒webshell的原理and设计 java c# .net php. 1. 隐蔽性 编辑 WebShell后门具有隐蔽性,一般有隐藏在正常文件中并修改文件时间达到隐蔽的,还有利用 ...

  10. Spark GraphX学习资料

    <Spark GraphX 大规模图计算和图挖掘> http://book.51cto.com/art/201408/450049.htm http://www.csdn.net/arti ...