在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里实现

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-12-25
  • 2022-01-24
  • 2022-03-04
  • 2022-02-18
猜你喜欢
  • 2022-12-23
  • 2022-03-02
  • 2022-12-23
  • 2021-10-24
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案