Geeks With Blogs
Dennis Bottjer | MVP

Windows Services are similar to UNIX daemons. They are applications that start when Windows boots and continue running until Windows shuts down. A Windows Service might be used to implement a timer that continually polls a source looking for new data. When new data is found an import process might be kicked off. .NET has made developing Windows Services much easier than past methods. As mentioned above a Windows Service is an application and as such requires installation. In the remainder of this post I would like to propose an alternative to developing a Windows Services for cases when an ASP.NET Application has already been developed. Including code within the Global.ASAX of an ASP.NET application to replace the task a Windows Service would perform eliminates the need to deploy two separate applications. In a large corporate environment keeping deployments as simple as possible is generally a good idea. The following code demonstrates how a delegate based server side timer can be implemented within the Global.asax of an ASP.NET application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Specialized;
using System.Threading;
using System.Web;
using AIS.Configuration;

namespace AIS.Reporting
{
public partial class Global : HttpApplication
{
#region Private Members
private ReportingService reportingService;
private int minutes = 1;
private static Timer reportTimer;
private int seconds = -1;
private bool enableReportProcessing;
#endregion

#region Methods: Timer

/// <summary>
/// Creates the timer and sets the callback if it is enabled
/// </summary>
public void InitializeTimer()
{
NameValueCollection appSettings = DynamicConfigurationManager.AppSettings;
enableReportProcessing = bool.Parse(appSettings["ReportTimerEnabled"]);
seconds = int.Parse(appSettings["ReportTimerSecondsInterval"]);
AutoResetEvent autoEvent = new AutoResetEvent(false);

if (reportTimer == null && enableReportProcessing)
{
reportTimer = new Timer(new TimerCallback(reportTimer_Callback), autoEvent, Interval, Interval);
}

}

/// <summary>
/// Internal call back which is responsible for firing IJob.Execute()
/// </summary>
/// <param name="state"></param>
private void reportTimer_Callback(object state)
{
ProcessReport();
}

        public void ProcessReport()
{
if (reportingService == null)
{
reportingService = new ReportingService();
}
reportingService.ProcessReport();
}



#endregion

        protected int Interval
{
get
{
if (seconds > 0)
return seconds * 1000;
return Minutes * 60000;
}
}

#region Public Properities

public int Minutes
{
get { return minutes; }
set { minutes = value; }
}

public bool EnableReportProcessing
{
get { return enableReportProcessing; }
set { enableReportProcessing = value; }
}

        public bool IsRunningProcessReport
{
get { return isRunningProcessReport; }
set { isRunningProcessReport = value; }
}

#endregion

void Application_Start(object sender, EventArgs e)
{
InitializeTimer();
}

}
}
Posted on Tuesday, February 27, 2007 6:45 AM ASP.NET , .NET , C# | Back to top


Comments on this post: Delegate Timers In ASP.NET

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © DennisBottjer | Powered by: GeeksWithBlogs.net