c#定时调用作业

ASP.NET应用中定时任务实现
本文介绍如何在ASP.NET应用程序中实现定时任务,通过在Global.asax文件中启动线程来定期执行特定操作。此外,还展示了如何利用Timer组件安排周期性任务,并通过ThConService_Control类管理这些任务。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值