Ben,
Here is a short example that actually comes from some of Rob Howard's sample
code:
using System;
using System.Web;
using System.Threadin g;
using System.Data;
using System.Data.Sql Client;
using System.Configur ation;
namespace BlackbeltBLL {
public class BackgroundServi ce : IHttpModule {
static Timer timer;
int interval = 5000;
public String ModuleName {
get { return "BackgroundServ ice"; }
}
public void Init(HttpApplic ation application) {
// Wire-up application events
if (timer == null)
timer = new Timer(new TimerCallback(S cheduledWorkCal lback),
application.Con text, interval, interval);
}
public void Dispose() {
timer = null;
}
private void ScheduledWorkCa llback (object sender) {
HttpContext context = (HttpContext) sender;
Poll(context);
}
void DoSomething (HttpContext context) {
}
#region DB Poll
void Poll (HttpContext context) {
SqlConnection connection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["Northwind"]);
SqlCommand command = new SqlCommand("SEL ECT
* FROM changenotificat ion", connection);
SqlDataReader reader;
string key = ConfigurationSe ttings.AppSetti ngs["SqlDepende ncy"];
connection.Open ();
reader = command.Execute Reader();
while (reader.Read()) {
string tableKey = String.Format(k ey, reader["Table"]);
if (context.Cache[tableKey] != null) {
int changeKey =
int.Parse( context.Cache[ String.Format(k ey, reader["Table"])].ToString() );
if (changeKey != int.Parse(
reader["ChangeID"].ToString() ))
context.Cache.R emove(tableKey) ;
}
}
connection.Clos e();
}
#endregion
}
}
--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"Ben Fidge" wrote:
Using Visual Studio 2005, how would I go about creating an application-wide
timer hosted in global.asax? There's certain processes I want to run every
couple of minutes and have had success using tiemrs this way with VS.NET
2003.
However, global.asax seems to have lost it's visual designer in the new VS
2005, so I tried manually creating a timer class in the Global()
constructor, but the Elapsed event doesn't fire. My code is as follows:
public class Global : HttpApplication
{
private System.Timers.T imer tmrMain;
public Global()
{
InitializeCompo nent();
tmrMain = new System.Timers.T imer(60000);
tmrMain.Enabled = true;
tmrMain.Elapsed += new
System.Timers.E lapsedEventHand ler(this.tmrMai n_Elapsed);
((System.Compon entModel.ISuppo rtInitialize) (tmrMain)).EndI nit();
}
private void tmrMain_Elapsed (object sender, System.Timers.E lapsedEventArgs
e) {
// Do functionality
}
}
Any ideas?
Ben