ASP.NET 2.0 IISTimer


Preliminaries

IIS is in general good web server, and it mostly answers the needs of web developers, but, sometimes , there is a need to perform some operations , after the server side of the page is finished already. There is Chronos in the Apache web server for such needs. IIS does not have such a mechanism, we at Tihnut.com thought about it, as a result, we  had developed IISTimer, which is based on the cache of IIS. With IISTimer you can set timers and stoppers. It has some inconvenient constraints, like it is impossible to set delay less ,than 10 seconds, there can be time margin in time delay up to 10 seconds, you need at least .NET 2 to compile the class library. There is a simple example project to demonstrate the usage and some functionality of IISTimer, and further, I am going to explain all the methods and properties of  the class.

Definitions

Using the class you can define two modes of using the objects : Timer and Stopper. Timer will perform the task every specific period of time, like timer in Visual Basic. Stopper will perform the task only once with specific delay, just like JavaScript SetTimeout function.

Name space: Tihnut.Web.Timer
Types included:

public delegate void. ExcpetionOccured(Exception exp,string methood);

The callback method called if any error while IISTimer instance existance

exp - exception ,that occurred
method - name of the method, where the exception occurred.
  
public delegate void TimerCallBack(DateTime time,string TimerName,IISTimer timer);

The callback method called , when the IISTimer instance delay is up.This one is named task.

time - timestamp when the  method was calledTimerName - name of the timer(stopper) which called the method
timer - the instance of IISTimer , to which the callback belongs.
public class NoException:Exception.The exception class, which actualy means, that no exception occurred.Used in creation methods.

Public class IISTimer

Static methods.

public static bool SetTimer(string TimerName,long Interval,TimerCallBack CallBack,out Exception FailureCause)

The method creates a timer. The method has one override.

TimerName - the name of the timer. Must be unique.
Interval - the delay in milliseconds.
CallBack - the callback method, which is called when the delay is up.
FailureCause - if the timer could not be created here will the exception, otherwise the exception will be of type NoException

public static bool SetTimer(string TimerName, long Interval,  TimerCallBack CallBack,ExcpetionOccured ExceptionCallBack,out Exception FailureCause)

The method creates a timer. The method has one override.

TimerName - the name of the timer. Must be unique.
Interval - the delay in milliseconds.
CallBack - the callback method, which is called when the delay is up.
ExceptionCallback - the callback method , which is called when any exception occurred, after successful timer creation during its lifetime.
FailureCause - if the timer could not be created here will the exception, otherwise the exception will be of type NoException

public static bool SetStopper(string TimerName, long Interval,  TimerCallBack CallBack, ExcpetionOccured ExceptionCallBack,out Exception FailureCause)

The method creates a stopper. The method has one override.

TimerName - the name of the stopper. Must be unique.
Interval - the delay in milliseconds.
CallBack - the callback method, which is called when the delay is up.
ExceptionCallback - the callback method , which is called when any
FailureCause - if the stopper could not be created here will the exception, otherwise the exception will be of typeNoException

public static bool SetStopper(string TimerName, long Interval, TimerCallBack CallBack,out Exception  FailureCause)

The method creates a stopper. The method has one override.

TimerName - the name of the stopper. Must be unique.
Interval - the delay in milliseconds.
CallBack - the callback method, which is called when the delay is up.
FailureCause - if the stopper could not be created here will the exception, otherwise the exception will be of typeNoException

public static IISTimer GetTimer(string TimerName)

The method retrieves by the unique timer(stopper)name, given at the creation. If the timer(stopper) does not exist, the method return null.

TimerName - unique timer(stopper) name.
  
public static bool Exists(string TimerName)

The method returns true if the timer(stopper) with the specific timer(stopper) name, given at the creation exists, otherwise false.

TimerName - unique timer(stopper).

public static bool RemoveTimer

The method removes the timer(stopper) with the specific timer(stopper) name. The method returns true if the operation succeded, otherwise false.

TimerName - the unique timer name.
  
The IISTimer enables an option of logging for debug purpose.

public static void StartLogging(string LogFileName)

The method starts to write logs for almost each operation of of IISTimer.
LogFileName - Name of the file to which the log is written.

public static void StopLogging()

The method stops logging.

Non static methods

public void Enable()

The IISTimer instance, which is used in timer or stopper modes can be in two different states: Enabled or Disabled. When the instance is enabled, it calls the TimerCallBack method , when delay is expired, otherwise it does not.The method sets the instance to the enabled state.

public void Disable()

The method sets the IISTimer instance to the state of disabled.

public void AddCallBack(TimerCallBack CallBack)

The method adds a TimerCallBack to the chain of callbacks, which is called when timer's(stopper's) delay is up(expired).

CallBack -
Timer's(stopper's) callback delegate

public void AddExceptionCallBack(ExcpetionOccured ExceptionCallBack)

The method adds an excpetion handler callbkack, which is called when any exception occurred , during the IISTimer instance lifetime.

ExceptionCallBack - On excpetion callback delegate.

public void ResetTimerCallBack(TimerCallBack CallBack)

The method changes the existing timer expiry callback method, by another.The method is called when the delay is up(expired).The new callback replaces all the previous calbacks series, if it existed.
CallBack - Timer(stopper) callback delegate.

public void ResetExceptionCallBack (ExcpetionOccured ExceptionCallBack)

The method replaces current on exception handler(callback) by another. The method is called when any exception is occurred during the timer's(stopper's) lifetime. The new callback replaces all the previous calbacks series, if it existed.

ExcpetionCallBack - On excpetion callback delegate.

Attributes

public string Name

Readonly property, gets the unique timer's(stopper's) name. Every instance of the IISTimer  should have its own internal unique name . It can be passed as parameter to the constructor, or to the static methods , which are responsible for instance creation.  

public bool IsEnabled

Readonly property, which gets the current state of the IISTimer instance: true - Enabled, false - Disabled.

public bool Infinite

The property gets or sets the mode in which the IISTimer is currently working or will work further.(true - timer mode, false - stopper mode).

public long Interval

The property sets or gets the current IISTimer instance delay value.


Similar Articles