brainimus
brainimus

Reputation: 11056

Start timer on web application start

I would like to start a System.Threading.Timer in my application when it launches (maybe deploy is the correct word). I have seen that you can use Application_Start() but this is only fired once the first request comes to the application. I need the timer to start as soon as the application is running so that it can check for work to process even if a user is not interacting with the site. How can I get the application to start the timer once it is up and running?

Upvotes: 1

Views: 4100

Answers (4)

azamsharp
azamsharp

Reputation: 20094

One way is to use the JavaScript setInterval method to start the timer. This needs to happen on every page so you will put it in the master page.

Upvotes: 0

Damien
Damien

Reputation: 686

This is normal behavior - the application needs to be "triggered" by someone requesting a page.

In IIS 7.5 and higher (e.g. win 2008/win7), there is a feature to do this automatically - see this link for details: http://visualstudiomagazine.com/articles/2010/02/18/preloading-aspnet-applications.aspx

In other ASP.NET web servers like the one my company makes (online at www.neokernel.com), this is accomplished by specifying certain pages that will be pre-loaded using the server configuration file. Essentially, the page is passed to the ASP.NET processing pipeline and compiled / spun up before anyone requests the URL associated with that page.

Upvotes: 0

Glennular
Glennular

Reputation: 18225

You can use the new ASP.Net 4 mode for AlwaysRunning. This way it won't wait for the first request to start

<applicationPools>
     <add name="MyAppWorkerProcess" managedRuntimeVersion="v4.0" 
         startMode="AlwaysRunning" />
</applicationPools> 

see: http://www.asp.net/learn/whitepapers/aspnet4

Upvotes: 8

JMarsch
JMarsch

Reputation: 21751

Application start actually does fire when the app is up and running. The problem that you are encountering is that IIS does not actually start the application until it receives the first request.

Another important thing to know: IIS can shut down your app due to inactivity. If you have something that you need to have running all the time, you might look for a different vehicle than an ASP.NET application. Perhaps a Windows service, or even a scheduled task.

Upvotes: 4

Related Questions