Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#
Article

IIS Timer

Rate me:
Please Sign up or sign in to vote.
2.80/5 (6 votes)
29 Jan 2008CPOL6 min read 36.3K   409   18   4
How to set timer in ASP.NET 2 applications

Introduction

First of all I would like to apologize for my English.

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.

Background

Definitions.<o:p>

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.<o:p>
<o:p>

Using the code

Name space : Tihnut.Web.TimerTypes included :<o:p>

<o:p>

public delegate void. ExcpetionOccured(Exception
exp,string methood);<o:p />

The callback method called if any error while IISTimer instance existance
exp - exception ,that occurred<o:p>
method – name of the method, where the exception occurred.<o:p>

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 called<o:p>
TimerName – name of the timer(stopper) which called the method
timer – the instance of IISTimer , to which the callback belongs.<o:p>

<o:p>

public class NoException:Exception.

The exception class, which actualy means, that no exception occurred.Used in creation methods.<o:p>

<o:p>

Public class IISTimer<o:p>

<o:p>

<o:p>

Static methods.<o:p>

<o:p>

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.<o:p>
Interval – the delay in milliseconds.<o:p>

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<o:p>

<o:p>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.<o:p>
Interval – the delay in milliseconds.<o:p>

CallBack – the callback method, which is called when the delay is up.<o:p>

ExceptionCallback – the callback method , which is called when any exception occurred, after successful timer creation during its lifetime.<o:p>

FailureCause – if the timer could not be created here will the exception, otherwise the exception will be of type NoException<o:p>

<o:p>

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.<o:p>

TimerName – the name of the stopper. Must be unique.<o:p>

Interval – the delay in milliseconds.<o:p>

CallBack – the callback method, which is called when the delay is up.<o:p>

ExceptionCallback – the callback method , which is called when any<o:p>

FailureCause – if the stopper could not be created here will the exception, otherwise the exception will be of type NoException<o:p>

<o:p>

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


The method creates a stopper. The method has one override.<o:p>

TimerName – the name of the stopper. Must be unique.<o:p>

Interval – the delay in milliseconds.<o:p>

CallBack – the callback method, which is called when the delay is up.<o:p>

FailureCause – if the stopper could not be created here will the exception, otherwise the exception will be of type NoException<o:p>

<o:p>

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.<o:p>

TimerName – unique timer(stopper) name.<o:p>

<o:p>

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.<o:p>

TimerName – unique timer(stopper).<o:p>

<o:p>

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.<o:p>

TimerName – the unique timer name.<o:p>

<o:p>

<o:p>

The IISTimer enables an option of logging for debug purpose.<o:p>

<o:p>
public static void StartLogging(string LogFileName)

The method starts to write logs for almost each operation of of IISTimer.<o:p>

LogFileName – Name of the file to which the log is written.<o:p>

public static void StopLogging()

The method stops logging.<o:p>

<o:p>

Non static methods<o:p>

<o:p>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.<o:p>

public void Disable()

The method sets the IISTimer instance to the state of disabled.<o:p>

public void AddCallBack(TimerCallBack
CallBack)<o:p> </o:p> 

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

CallBack – Timer's(stopper's) callback delegate<o:p>

<o:p>

public void
AddExceptionCallBack(ExcpetionOccured ExceptionCallBack)

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

ExceptionCallBack – On excpetion callback delegate.<o:p>

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.<o:p>

CallBack – Timer(stopper) callback delegate.<o:p>

<o:p>

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.<o:p>

ExcpetionCallBack – On excpetion callback delegate.<o:p>

<o:p>

Attributes<o:p>

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.

<o:p>
public bool IsEnabled

Readonly property, which gets the current state of the IISTimer instance: true – Enabled, false – Disabled.<o:p>

<o:p>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).<o:p>

<o:p>public long Interval

The property sets or gets the current IISTimer instance delay value.<o:p>

<h2>Points of Interest </h2>
In a case you had discovered any bug, or you would like to send some opinion to Tihnut.com,

or ask question the componenent devoloper about the component, please visit the :http://www.tihnut.com/default.aspx?opt=pp&id=2

    
<h2>History </h2>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy? Pin
J4amieC29-Jan-08 4:36
J4amieC29-Jan-08 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.