NET开发日记11

用C#限时

首先引入命名空间
using System.Timers;
using System.IO;

在 Global.asax 中加入静态对象(变量)
public static Timer gtimer = null;  // 定义全局定时器类对象
public static int gcount = 0;  //  测试用计时器变量

在 Global.asax 中的 Application_OnStart 事件过程中定义计时器,代码如下:
protected void Application_Start(Object sender, EventArgs e)
  {
    // 创建一个计时器,单位:毫秒
    gtimer = new Timer(1000);
    // 将自定义用户函数(TimerEventFunction)指定为计时器的 Elapsed 事件处理程序
    // TimerEventFunction 可以写入自己的需要执行的代码逻辑
    gtimer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerEventFunction);
    // AutoReset 属性为 true 时,每隔指定时间间隔触发一次事件
    // 若赋值 false,则只执行一次
    gtimer.AutoReset = true;
    gtimer.Enabled = true;
  }

  TimerEventFunction 函数代码如下:
    // 定时器定时触发控件
  protected void TimerEventFunction(Object sender, ElapsedEventArgs e)
  {
   // 这里写入自定义代码
   gcount += 1;
   FileInfo file = new FileInfo("C://" + gcount.ToString() + ".txt");
   file.Create();  //    创建文件,定时执行此过程将不停的创建文件
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值