Setting up a timer in Global.asax

B

Ben Fidge

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.Timer tmrMain;

public Global()
{
InitializeComponent();

tmrMain = new System.Timers.Timer(60000);
tmrMain.Enabled = true;
tmrMain.Elapsed += new
System.Timers.ElapsedEventHandler(this.tmrMain_Elapsed);
((System.ComponentModel.ISupportInitialize) (tmrMain)).EndInit();
}

private void tmrMain_Elapsed(object sender, System.Timers.ElapsedEventArgs
e) {
// Do functionality
}
}

Any ideas?

Ben
 
G

Guest

Ben,
Here is a short example that actually comes from some of Rob Howard's sample
code:

using System;
using System.Web;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BlackbeltBLL {
public class BackgroundService : IHttpModule {
static Timer timer;
int interval = 5000;
public String ModuleName {
get { return "BackgroundService"; }
}

public void Init(HttpApplication application) {
// Wire-up application events
if (timer == null)
timer = new Timer(new TimerCallback(ScheduledWorkCallback),
application.Context, interval, interval);
}

public void Dispose() {
timer = null;
}

private void ScheduledWorkCallback (object sender) {
HttpContext context = (HttpContext) sender;
Poll(context);
}

void DoSomething (HttpContext context) {
}

#region DB Poll
void Poll (HttpContext context) {
SqlConnection connection = new
SqlConnection(ConfigurationSettings.AppSettings["Northwind"]);
SqlCommand command = new SqlCommand("SELECT
* FROM changenotification", connection);
SqlDataReader reader;
string key = ConfigurationSettings.AppSettings["SqlDependency"];
connection.Open();
reader = command.ExecuteReader();
while (reader.Read()) {
string tableKey = String.Format(key, reader["Table"]);
if (context.Cache[tableKey] != null) {
int changeKey =
int.Parse( context.Cache[ String.Format(key, reader["Table"])].ToString() );
if (changeKey != int.Parse(
reader["ChangeID"].ToString() ))
context.Cache.Remove(tableKey);
}
}
connection.Close();
}
#endregion
}
}

--Peter
 
G

Guest

Good idea! Probably wouldn't thought of using a HttpModule.

Thanks

Ben

Peter Bromberg said:
Ben,
Here is a short example that actually comes from some of Rob Howard's sample
code:

using System;
using System.Web;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BlackbeltBLL {
public class BackgroundService : IHttpModule {
static Timer timer;
int interval = 5000;
public String ModuleName {
get { return "BackgroundService"; }
}

public void Init(HttpApplication application) {
// Wire-up application events
if (timer == null)
timer = new Timer(new TimerCallback(ScheduledWorkCallback),
application.Context, interval, interval);
}

public void Dispose() {
timer = null;
}

private void ScheduledWorkCallback (object sender) {
HttpContext context = (HttpContext) sender;
Poll(context);
}

void DoSomething (HttpContext context) {
}

#region DB Poll
void Poll (HttpContext context) {
SqlConnection connection = new
SqlConnection(ConfigurationSettings.AppSettings["Northwind"]);
SqlCommand command = new SqlCommand("SELECT
* FROM changenotification", connection);
SqlDataReader reader;
string key = ConfigurationSettings.AppSettings["SqlDependency"];
connection.Open();
reader = command.ExecuteReader();
while (reader.Read()) {
string tableKey = String.Format(key, reader["Table"]);
if (context.Cache[tableKey] != null) {
int changeKey =
int.Parse( context.Cache[ String.Format(key, reader["Table"])].ToString() );
if (changeKey != int.Parse(
reader["ChangeID"].ToString() ))
context.Cache.Remove(tableKey);
}
}
connection.Close();
}
#endregion
}
}

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Ben Fidge said:
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.Timer tmrMain;

public Global()
{
InitializeComponent();

tmrMain = new System.Timers.Timer(60000);
tmrMain.Enabled = true;
tmrMain.Elapsed += new
System.Timers.ElapsedEventHandler(this.tmrMain_Elapsed);
((System.ComponentModel.ISupportInitialize) (tmrMain)).EndInit();
}

private void tmrMain_Elapsed(object sender, System.Timers.ElapsedEventArgs
e) {
// Do functionality
}
}

Any ideas?

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top