tags:

views:

207

answers:

5

Hi,

I'm building a site that runs on a timed competition. Every n amount of hours, the competition ends.

Where would I put code that runs on a timed interval?

I was thinking of checking each time a page request is made, but I was looking for another way.

Thanks for your time.

A: 

If you have full access to your database server, you could do something simple like create a SQL Agent job that runs on a 1 hour interval calling a procedure that would set the active competition id, off of which your other database procedures could work from.

jkelley
I have godaddy hosting. Database access is limited.
Sir Psycho
A: 

ASP.NET is a technology used to help you create web pages which are served on a per-request basis. Thus, it is not particularly well suited to discretely timed execution. There are solutions (such as checking per-request, as you mention), but why not create a simple timed process using a C# console or WinForms application to run on your server, using the same database?

JoshJordan
I have limited access to the web server. Are there any background thread type implementations i could use?
Sir Psycho
A: 

I think this would be well suited to be done in a separate dll that tracks the time for a competition and then have the asp.net pages on request request if the time is up. I think trying to track this in the asp.net page itself will prove very difficult and hard to maintain.

klabranche
+1  A: 

If you are really limited by your hosting you might want to try out this approach: http://www.apterasoftware.com/Blog/Post/09-04-09/Scheduled_ASP_NET_Task_ASP_NET_Cron_Jobs.aspx

It's a bit of a hack, but saves you money on hosting! :)

russau
Awesome! Goes to show how using google differenly yields different results.Thanks :-)
Sir Psycho
New link for that blog:http://www.apterasoftware.com/Blog/Post/09-04-09/Scheduled_ASP_NET_Task_ASP_NET_Cron_Jobs.aspx
Mark Elder
A: 

A couple of ideas:

  • Create a class that sets up a timer and specifies a callback or an event handler that will get called when the timer elapses. You can create an instance of that class on application start, session start, or in response to an action from a page depending on how and when you want the timer to start. You can then add that instance to ASP.NET session or application state. In the timer elapsed callback, the object can remove itself from application or session dictionary and add another value in there with a known key to indicate that the time is up. Then each page can get this known key from application or session state and check its value. If it is set, the time is up. Otherwise, there is still time so let the user continue.
  • Set up a FileSystemWatcher in your ASP.NET application and monitor changes on a specific file. Then have another process with a timer that touches that file when the timer expires. The file changed event handler can then set up some variable visible by pages or stick something into ASP.NET application state and each page pull it out and check to see if the time is up. You can use that other process as an admin tool to manage exam durations.
Mehmet Aras
I like your first idea, the second one seems dodgy :-) Thanks for the reply nonetheless :)
Sir Psycho