0
  • ASP.NET 4.51 WebForms, VS2013

Within the application I want it to wake up periodically and run some or other tasks. I am guessing that this would be done by calling a particular URL within the application and that ASPX page does the actual work.

My question is how do I trigger this? What I am looking for is something to a cron job that is "invoked" by the WevForms application periodically.

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • possible duplicate of [Long-running ASP.NET tasks](http://stackoverflow.com/questions/2519756/long-running-asp-net-tasks) – John Saunders Apr 01 '14 at 04:41

3 Answers3

1

What you are describing sounds like the initial bad approach any new developer on ASP. Net asks himself (well, at least many of us do at some point). I would recommend you re-evaluate your strategy before investing time on this.

Nevertheless, should you choose to continue through this path, this Timer class will get the job done. Use it on your Global.asax.cs file on the Application start method.

http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

Don't forget to stop the timer on the application stop method too.

DanielCuadra
  • 970
  • 9
  • 17
  • I need to have a background scheduler to run some tasks periodically. This is not for the client side. Not sure why this is a bad idea. Or are you suggesting that I use the OS scheduler to invoke a HTTP end point in the application? – TheEdge Apr 01 '14 at 04:38
  • The Timer class will do what you need. Nevertheless, you'll either lose control over the background task or you will end up writing a bunch of code to have control on it. I would better write a Windows service that performs the background tasks so you can control it with ease. Using the Timer class is not bad, only if you can truly isolate your background task in a way you can start it and forget about it (the caching block library actually uses the Timer class, so, it's doable, just be careful) – DanielCuadra Apr 01 '14 at 05:58
0

using Two easy steps, you can achieve your goal.

  1. Create JavaScript timer
  2. Create Handler Page (.ASHX)

Write all your business logic into handler page and call this handler page at specific period of time using javascript timer.

Ref:

  1. JavaScript Timer
  2. Generic Handler

Note: This timer should be in Master page file. Other option to use timer is to put it in code behind of master page and call handler in elapsed event of timer.

SpiderCode
  • 10,062
  • 2
  • 22
  • 42
  • I am not looking to run a timer when the user has a browser open. I am looking to run this on the server independent of whether someone is accessing the site. – TheEdge Apr 01 '14 at 04:36
  • then other option is to create a webservice or API in that application and create windows service or scheduler which call this webservice at specific period of time. – SpiderCode Apr 01 '14 at 04:58
0

I ended up using http://www.quartz-scheduler.net/ which is a lot more robust and feature rich than the potential solutions presented above.

TheEdge
  • 9,291
  • 15
  • 67
  • 135