用Thread做点自动化的事

本文介绍了一个简单的自动计数任务实现方案,通过C#语言创建后台服务,在特定的时间间隔内更新计数并显示结果。该服务利用了线程来实现定时操作,并通过Web页面展示最新的计数状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值