Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

July 2010 - Posts

Using the ASP.NET Cache as a Scheduler

UPDATED

By using the ASP.NET cache, it is very easy to create a simple scheduling mechanism, for example, one that fires an event every X minutes. I once wrote a simple module to do just this, here is its code. It is up to you to enhance it to your needs!


public class SchedulerModule: IHttpModule
	{
		#region Public constructor
		public SchedulerModule()
		{
			this.Enabled = true;			
			this.Interval = TimeSpan.FromMinutes(1);	//fires every minute
			this.FirstTime = true;
		}
		#endregion

		#region Public properties
		public Boolean Enabled
		{
			get;
			set;
		}

		public TimeSpan Interval
		{
			get;
			set;
		}

		public Boolean FirstTime
		{
			get;
			private set;
		}
		#endregion

		#region Public events
		public event EventHandler Tick;
		public event EventHandler Start;
		public event EventHandler Stop;
		#endregion

		#region Protected methods
		protected void StartScheduler()
		{
			CacheItemRemovedCallback callback = null;
			HttpContext context = HttpContext.Current;

			callback = delegate(String key, Object value, CacheItemRemovedReason reason)
			{
				if (reason == CacheItemRemovedReason.Expired)
				{
					if (this.Enabled == true)
					{
						this.StartRunning(callback, context);
					}

					if (this.FirstTime == true)
					{
						this.FirstTime = false;
						this.OnStart(EventArgs.Empty);
					}

					this.OnTick(EventArgs.Empty);

					if (this.Enabled == false)
					{
						this.OnStop(EventArgs.Empty);
					}
				}
			};

			this.StartRunning(callback, context);
		}

		protected void StartRunning(CacheItemRemovedCallback callback, HttpContext context)
		{
			context.Cache.Add(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DateTime.Now + this.Interval, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, callback);
		}
		#endregion

		#region Protected virtual methods
		protected virtual void OnStart(EventArgs e)
		{
			EventHandler handler = this.Start;

			if (handler != null)
			{
				handler(this, EventArgs.Empty);
			}
		}

		protected virtual void OnStop(EventArgs e)
		{
			EventHandler handler = this.Stop;

			if (handler != null)
			{
				handler(this, EventArgs.Empty);
			}
		}

		protected virtual void OnTick(EventArgs e)
		{
			EventHandler handler = this.Tick;

			if (handler != null)
			{
				handler(this, EventArgs.Empty);
			}
		}
		#endregion

		#region IHttpModule Members

		public void Dispose()
		{
			this.Enabled = false;	
		}

		public void Init(HttpApplication context)
		{
			this.StartScheduler();
		}

		#endregion
	}


It exposes two properties:

  • Enabled: if the scheduler is active
  • Interval: the firing interval

As for the installation, you just have to reference it on your web.config:

    
    
      
    
    
    
      
        
      
    

There's two ways in which you can use it: by manually wiring an event, maybe on Application_Start, or by writing special methods, use one or the other, not both!


protected void Application_Start(Object sender, EventArgs e)
{
	//Note: you do not have to do this if you use the following methods!	
	//SchedulerModule schedulerModule = HttpContext.Current.ApplicationInstance.Modules.OfType<SchedulerModule>().Single();
        SchedulerModule schedulerModule = this.Modules.OfType<SchedulerModule>().Single();

	//Set the firing interval
	schedulerModule.Interval = TimeSpan.FromMinutes(5);
	
	schedulerModule.Start += this.Scheduler_Start;
	schedulerModule.Stop += this.Scheduler_Stop;
	schedulerModule.Tick += this.Scheduler_Tick;
}

protected void Scheduler_Start(Object sender, EventArgs e)
{
}

protected void Scheduler_Stop(Object sender, EventArgs e)
{
}

protected void Scheduler_Tick(Object sender, EventArgs e)
{
}

Bookmark and Share
More Posts