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); } }