473,978 Members | 10,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting up a timer in Global.asax

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
Feb 26 '06 #1
2 9658
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

Feb 26 '06 #2
Good idea! Probably wouldn't thought of using a HttpModule.

Thanks

Ben

"Peter Bromberg [C# MVP]" wrote:
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

Feb 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2119
by: Denon | last post by:
How to terminate a Session in Global.asax? 1)In Global.asax, I have store every sessionID into a global array 2) I have start a timer while application_start event, this timer lookup something 3) For some time limit running out, then timer trigger want to kill the session by specified sessionID. How to implement 3)? Thanks for any advices
1
2489
by: Ram | last post by:
(I am running .NET Framework 1.1) I have a timer(System.Timers.Timer) running in my Global.asax page. In the timer.elapsed event I am trying to access an object I stored in the Cache. HttpContext.Current.Cache("xxx"). I am getting an error saying that HttpContext.Current is nothing. I also tried to check HttpContext.Current.User.Identity.Name and got an error and it says HttpContext.Current.User is nothing.
9
7895
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I might need to carry out some back-end processing without pausing the individual users' Session while that process runs. E.g. I might provide the ability for a user to upload a text file of job
5
6969
by: Piz | last post by:
I've read a previous discussion about the same topic, but there's a difference. I call HttpContext.Current.Response.Redirect("file.txt") from a ownmade sub in the global.asax. That doesn't works, i'm quite new in asp.net and so i don't know what a studip mistake i'm doing!! here there's global asax code: <%@ Application Language="VB" %>
11
8399
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the generated code on a W2K3 server the application_start and any other event on the global.asax file won't fire. I added tracing and logging to make sure and I can see the code is just not execution. When running the same exact (deployed etc.) code on...
2
1912
by: rgparkins | last post by:
Hi Guys Maybe a simple and easy solution to this, but I keep getting exception object not set to instance etc etc. I have a Timer that is initiated in Application_Start in Global.asax. This timer I am using as a service every 10 minutes to send email to users. OK so all is fine until I need to load a file from the server (in fact an email template).
4
5204
by: Larry Epn | last post by:
Simple question: I have a c# asp.net project that was given to me. It has the c# code within the <scriptsection of the global.asax file. I would rather have it in separate files; e.g., global.asax.cs; So, I deleted the existing global.asax file (there was nothing important in it yet), and went to add a new one through VS2005. I chose the "Global Application Class" (Global.asax) item from the list of new items. The "Place code in...
8
5790
by: Victor | last post by:
Can I get the events in GLOBAL.ASAX to fire if a classic ASP page is being accessed by the user?
2
1841
by: Mubi | last post by:
I am using VS2003 and developing web services. We also need a timed activity to be performed on the server. If i drag a timer and place it on global.asax, add interval and timer handler. it doesnot work. Is there anyone with the answer. How could i achieve this functionality.
0
10359
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11836
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11433
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8467
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6426
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.