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 } }
  • 相关阅读:
    VS 2008潜在强大的功能:提取EXE文件中的ICO等资源
    园友们注意:淘宝网上QQ会员 4钻 3元 等都为骗子行为
    Comet Async Process Request Handler
    WCF(Sender) to MSMQ to WCF(Receiver)
    ASP.NET Web Form GridView DetailsView Query Edit
    WCF NetTcp AsyncQueue Service
    Xml CDATA 序列化
    Sync Invoke Remoting Async Invoke
    .Net 4.0 Remoting ConcurrentQueue
    Socket Async Receive Data to LinkedList Buffer (telnet proxy server)
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/6554908.html
Copyright © 2011-2022 走看看