net中的定时器

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

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

直接上代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace HMYY.Config
{
    public class Class1
    {
        static System.Timers.Timer timer = new System.Timers.Timer(30);
        static int a = 1;
        static Class1()
        {
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //执行你的定时代码
            a = a + 1;
            
        }

        public static int  Geta()
        {
            return a;
        }
    }
}

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

posted @ 2010-11-09 10:00  二锅头  阅读(378)  评论(1编辑  收藏  举报