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 } }
  • 相关阅读:
    解决pip3的ImportError: cannot import name 'main'
    linux 安装Python3.6
    Linux安装redis和部署
    redis密码管理
    CentOS7使用firewalld打开关闭防火墙与端口
    scrapy 从Windwos平台移植到 Linux平台之实操
    Linux 环境下安装Maven
    解决:安装Jenkins时web界面出现该jenkins实例似乎已离线
    持续集成工具Jenkins结合SVN的安装和使用
    Linux下的SVN服务器搭建
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/6554908.html
Copyright © 2011-2022 走看看