zoukankan      html  css  js  c++  java
  • c#定时调用作业

    1.在Global.asax文件中添加启动线程

      protected void Application_Start(object sender, EventArgs e)
      {
            
           Thread ThService = new Thread(new ThreadStart(Web.Common.ThConService_Control.Instance().Start));
           ThService.Start();
          
      }

    2.创建ThConService_Control.cs文件。

    namespace Web.Common
    {
        public class ThConService_Control
        {
            #region Timer 计时器定义
            /// <summary>
            /// 时间间隔,10*(60秒)10分钟
            /// </summary>
            private static int Interval = 1 * 5 * 1000;//saitor
            private long Times=0;
            private Timer WebTimerObj = null;
            /// <summary>
            /// 是否正在执行中
            /// </summary>
            private int _IsRunning;
    
            /// <summary>
            /// 实例化一个对象
            /// </summary>
            private static readonly ThConService_Control _WebTimerTask = null;
    
            /// <summary>
            /// 
            /// </summary>
            static ThConService_Control()
            {
                _WebTimerTask = new ThConService_Control();
            }
    
            /// <summary>
            /// 实例化
            /// </summary>
            /// <returns></returns>
            public static ThConService_Control Instance()
            {
                return _WebTimerTask;
            }
    
            /// <summary>
            /// 开始
            /// </summary>
            public void Start()
            {
                if (WebTimerObj == null)
                {
                    WebTimerObj = new Timer(new TimerCallback(WebTimer_Callback), null, Interval, Interval);
                }
            }
            /// <summary>
            /// 停止
            /// </summary>
            public void Stop()
            {
                if (WebTimerObj != null)
                {
                    WebTimerObj.Dispose();
                    WebTimerObj = null;
                }
            }
            /// <summary>
            /// WebTimer的主函数
            /// </summary>
            /// <param name="sender"></param>
            private void WebTimer_Callback(object sender)
            {
                try
                {
                    if (Interlocked.Exchange(ref _IsRunning, 1) == 0)
                    {
                        ExecuteMain();
                        Times++;
                        Times = (Times % 100000);
                    }
                }
                catch
                {
    
                }
                finally
                {
                    Interlocked.Exchange(ref _IsRunning, 0);
                }
            }
    
            #endregion
    
            #region 实际执行的方法
    
            public List<LiEntity> Lst = new List<LiEntity>();
            BL Bll = new BL();
            BLLogs objManage_Log = new BLLogs();
            public Object SyncObj = new Object();
    
            /// <summary>
            /// 实际执行的方法
            /// </summary>
            private void ExecuteMain()
            {
              
    
                if (Lst.Count > 0)
                {
                    lock (SyncObj)
                    {
                        while (Lst.Count > 0)
                        {
                  //对队列数据进行处理 }
    } } } #endregion } }
  • 相关阅读:
    【2020省选Day1T1】LOJ3299 「联合省选 2020 A | B」冰火战士
    题解 CF1369 D,E,F Codeforces Round #652 (Div. 2)
    题解 LOJ3298 「BJOI2020」封印(SAM,数据结构)
    题解 nflsoj99 牛顿的烈焰激光剑(容斥,DP,数学)
    判断长度为3的等差数列(经典问题)
    树形图求和:一道经典矩阵知识题
    题解 LOJ2390 「JOISC 2017 Day 1」开荒者
    istio sidecar自动注入过程分析
    filebeat-kafka日志收集
    istio路由配置
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/6554908.html
Copyright © 2011-2022 走看看