zoukankan      html  css  js  c++  java
  • 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里实现

  • 相关阅读:
    Redis篇
    MySql篇
    Tomcat篇
    JDK篇
    冒泡排序(算法源码)
    堆排序(源码)
    快速排序(递归及非递归算法源码)
    MongoDB 复制
    MongoDB appendix
    服务器端脚本
  • 原文地址:https://www.cnblogs.com/linjiancun/p/1872340.html
Copyright © 2011-2022 走看看