• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
    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);
        }
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中显/隐式实现接口及其访问方法发布时间:2022-07-14
下一篇:
C#ABP允许跨域请求发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap