用Thread做点自动化的事
Target: automatic count.
当然, 为了简洁, 取最简洁的方式。
项目结构:
1.Auto类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace BackPlatform{ public class Auto { public static string msg=""; public static void ThreadRun(){ ParameterizedThreadStart pts = new ParameterizedThreadStart(CountBill); Thread thread = new Thread(pts); thread.Start(); } public static void CountBill(object dt) { while (true) { DateTime dt2 =DateTime.Now; //夜间统计数据时使用 //if (dt2.Hour == 0 && dt2.Minute == 30) msg =dt2.Minute%2==0? dt2.ToString()+"<font color='red'>到点了,开始统计数据!</font>" :dt2.ToString()+"<font color='blue'>没有到点</font>"; Thread.Sleep(60000);//暂停60秒, 减少资源占用 } } }}
2.Global.asax
<%@ Application Language="C#" %><mce:script runat="server"><!-- void Application_Start(object sender, EventArgs e) { //在应用程序启动时运行的代码 BackPlatform.Auto.ThreadRun(); } void Application_End(object sender, EventArgs e) { //在应用程序关闭时运行的代码 } void Application_Error(object sender, EventArgs e) { //在出现未处理的错误时运行的代码 } void Session_Start(object sender, EventArgs e) { //在新会话启动时运行的代码 } void Session_End(object sender, EventArgs e) { //在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式 //设置为 StateServer 或 SQLServer,则不会引发该事件。 }// --></mce:script>
3.Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <meta http-equiv="Refresh" content="2" /><%-- 页面定期刷新,如果加url的,则会重新定向到指定的网页,content后面跟的是时间(单位秒),把这句话加到指定网页的<head></head>里一般也用在实时性很强的应用中,需要定期刷新的如新闻页面,论坛等,不过一般不会用这个,都用新的技术比如ajax等--%> <title>无标题页</title></head><body> <form id="form1" runat="server"> <div> <%=BackPlatform.Auto.msg %> </div> </form></body></html>