Line 1: using System;
Line 2: using System.Data;
Line 3: using System.Configuration;
Line 4: using System.Web;
Line 5: using System.Web.Security;
Line 6: using System.Web.UI;
Line 7: using System.Web.UI.WebControls;
Line 8: using System.Web.UI.WebControls.WebParts;
Line 9: using System.Web.UI.HtmlControls;
Line 10: using System.Threading;
Line 11:
Line 12: namespace CMS.Automation
Line 13: {
Line 14: /// <summary>
Line 15: /// Summary description for Scheduler
Line 16: /// </summary>
Line 17: public sealed class Scheduler
Line 18: {
Line 19: // HttpContext
Line 20: private static HttpContext currentContext = null;
Line 21:
Line 22: // evento
Line 23: public static event EventHandler Tick;
Line 24: public static event EventHandler MinuteChanged;
Line 25: public static event EventHandler HourChanged;
Line 26: public static event EventHandler DayChanged;
Line 27: public static event EventHandler WeekChanged;
Line 28: public static event EventHandler MonthChanged;
Line 29: public static event EventHandler YearChanged;
Line 30:
Line 31: // timer
Line 32: private static Timer mTimer = null;
Line 33:
Line 34: // ctor
Line 35: private Scheduler()
Line 36: { }
Line 37:
Line 38: // callback
Line 39: static void Timer_Callback(object state)
Line 40: {
Line 41: OnTick(EventArgs.Empty);
Line 42: }
Line 43:
Line 44: // gestore evento
Line 45: private static void OnTick(EventArgs e)
Line 46: {
Line 47: EventHandler handler = Tick;
Line 48:
Line 49:
Line 50: if (handler != null)
Line 51: handler(currentContext, e);
Line 52:
Line 53: // check minute changed
Line 54: if (DateTime.Now.Second == 0)
Line 55: {
Line 56: if(MinuteChanged != null)
Line 57: MinuteChanged(currentContext, e);
Line 58: // check hour changed
Line 59: if (DateTime.Now.Minute == 0)
Line 60: {
Line 61: if(HourChanged != null)
Line 62: HourChanged(currentContext, e);
Line 63: // check day changed
Line 64: if (DateTime.Now.Hour == 0)
Line 65: {
Line 66: if(DayChanged != null)
Line 67: DayChanged(currentContext, e);
Line 68:
Line 69: // chech week changed
Line 70: if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
Line 71: if(WeekChanged != null)
Line 72: WeekChanged(currentContext, e);
Line 73:
Line 74: // check month changed
Line 75: if (DateTime.Now.Day == 1)
Line 76: {
Line 77: if(MonthChanged != null)
Line 78: MonthChanged(currentContext, e);
Line 79: // check year changed
Line 80: if (DateTime.Now.Month == 1)
Line 81: if(YearChanged != null)
Line 82: YearChanged(currentContext, e);
Line 83: }
Line 84: }
Line 85: }
Line 86: }
Line 87: }
Line 88:
Line 89: // avvia il timer
Line 90: public static void Start(HttpContext currentContext, int interval)
Line 91: {
Line 92: lock (typeof(Scheduler))
Line 93: {
Line 94: Scheduler.currentContext = currentContext;
Line 95: if (mTimer == null)
Line 96: mTimer = new Timer(
Line 97: new TimerCallback(Timer_Callback),
Line 98: null,
Line 99: interval,
Line 100: interval);
Line 101: }
Line 102: }
Line 103:
Line 104: // ferma il timer
Line 105: public static void Stop()
Line 106: {
Line 107: lock (typeof(Scheduler))
Line 108: if (mTimer != null)
Line 109: {
Line 110: mTimer.Dispose();
Line 111: mTimer = null;
Line 112: }
Line 113: }
Line 114:
Line 115: // informa se il timer è avviato
Line 116: public static bool IsStarted
Line 117: {
Line 118: get { return mTimer != null; }
Line 119: }
Line 120: }
Line 121: }
|