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 } }
原文地址:https://www.cnblogs.com/TBW-Superhero/p/6554908.html