`
raisun_1988
  • 浏览: 113791 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

net中的定时器

    博客分类:
  • .Net
阅读更多

在web中某个类中对某个方法我想让它定时执行。

对于此,我们可以利用类的静态构造函数和定时器来实现。

直接上代码:

 

01 using System;
02 using System.Collections.Generic;
03 using System.Text;
04   
05 namespace HMYY.Config
06 {
07     public class Class1
08     {
09         static System.Timers.Timer timer = new System.Timers.Timer(30);
10         static int a = 1;
11         static Class1()
12         {
13             timer.AutoReset = true;
14             timer.Enabled = true;
15             timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
16             timer.Start();
17         }
18   
19         static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
20         {
21             //执行你的定时代码
22             a = a + 1;
23               
24         }
25   
26         public static int  Geta()
27         {
28             return a;
29         }
30     }
31 }

 

这样的话简单的定时器就做好了,如果做全局的,可以在Global.asax里实现

分享到:
评论
1 楼 agameplay 2010-11-12  
呵呵,感谢,一直不知道会员定期过期检测,这下有法了,以前以为Timer不能用于WEB中呢。

相关推荐

Global site tag (gtag.js) - Google Analytics