Tuesday, March 13, 2012

system.threading question

Hey everyone...

I don't know exactly where to post this to, so i'll try here first.

Focussing on the system.threading.timer object...

Where would i place this timer and assocaited delegates in an asp.net web application?

I need it to simply process the files in a directory on
a shared folder, but i need it to run every 10 minutes or so,
irregardless of someone has loaded a page or not - the server
still needs to process the files...

thanks
tonyYou will need to develop a windows service. You can not have a timer run without a asp.net application behind it. The asp.net application gets created on the first request and stays around for a while.

If you need this processing to happen irregardless if a web request comes in or not, then you can not host this process in a asp.net application.

HTH,

bill
Why not make a Windows service?
Some months back, Rob Howard gave a talk at the user group I attend and covered a wide variety of topics - one of the things he showed us was background thread processing implemented for an ASP.NET app. You can find a link to the download of the code on his site (http://www.rob-howard.net/), click on the "Demos" link under "Blackbelt ASP.NET" (not the "Blackbelt ASP.NET Controls"). In the "BlackbeltBLL" folder in the code you download, you will find a file, "HttpModule.cs;" here is the code (modified to include only what you need):


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 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)
{
// here you call whatever code you want to run at the interval
ProcessFiles();
}
}
}

Add that to your project and of course register it with the web.config file.

Basically, this starts a background thread that will run at whatever interval you specify and perform some task(s). I have extended and used this for a variety of applications, and it works nicely.

HTH
Good question...

That is how i have it implemented now...

but if i can streamline the installation, remove a few tasks
from the installation/implementation team, that would save
bottom line cash - and when you save a little time here and there, and
the installation/implementation team says the install took
20 less minutes, then the management sees that as a good thing.

so to answer your question - i'm sick of writing windows services...

thanks for the reply

take care
tony
Let me know if that ends up working for you (the HttpModule) . . .
Hi Jason and everyone!!

Your code snippet looks like a winner..

and the rob howard site is a fantastic reference - thank you

i will create a working demo in a few minutes -

if there are any problems, i'll post them up!!

thanks again -

take care
tony
Sweet, I hope it works out for you . . . I love it. Not that anyone cares, but I added this HttpModule to my installation of nGallery for my family site, and it emails all of my family members when anyone uploads new images so that we know when to go look at the site. I have had that up and running for months and love the convenience of not having to run a separate service.
Hi Jason,

Where exactly do i hook this new class from??

i was thinking the
protected void Application_Start(Object sender, EventArgs e)
in the global.asax section...

but i'm not sure if this is the proper place to kick it off from...

advise would be greatly appreciated

take care
tony
Tony,

Check out the section with the heading "Creating and Registering Custom HTTP Modules and HTTP Handlers" inthis MSDN article for more information on how to register HttpModules (about 1/4 of the way down the page).

Let me know if there is anything else . . .

0 comments:

Post a Comment