C# Global定时执行Global文件aTimer处理

    public class Global : System.Web.HttpApplication
    {
        private static event Action eventActions;
        /// <summary>
        ///  当前正在运行的异步线程池
        /// </summary>
        private static Dictionary<string, IAsyncResult> diyAsync = new Dictionary<string, IAsyncResult>();
        protected void Application_Start(object sender, EventArgs e)
        {
            eventActions += MyClass.Writer;//任务(可以处理多个方法)
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 200;
            aTimer.Enabled = true;
        }
        /// <summary>
        /// 定时执行任务
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            foreach (Action action in eventActions.GetInvocationList())
            {
                try
                {
                    if (diyAsync.Any(p => p.Key == action.Method.Name) && !diyAsync[action.Method.Name].IsCompleted)
                        continue;
                    diyAsync[action.Method.Name] = action.BeginInvoke(null, null);
                }
                catch (Exception ex)
                {
                    //任务执行出错
                }
            }
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
           string name=System.Reflection.MethodBase.GetCurrentMethod().Name;
           WH_WriteLog.WriteLog(name);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            string name=System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
            WH_WriteLog.WriteLog(e.ToString());
        }

        protected void Session_End(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }

        protected void Application_End(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }
    }

    public class MyClass
    {
        public static void Writer()
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }
    }

 

转载于:https://www.cnblogs.com/-hao/p/9944813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值