views:

201

answers:

5

How to execute asp.NET code in scheduled task of windows?

A: 

The simplest and probably the easiest way to do this is to make the code a command line app and then execute it using the Windows Scheduler found in Control Panel.

Cobusve
A: 

There is no way to schedule ASP.NET code from within a web application, because the web is stateless.

My preferred method to schedule .NET code, is to write a windows service that I install on the web server. See. http://blog.dogma.co.uk/search/label/windowsservice.

An quicker, less efficient, way is to add the code to a page and execute that page from Windows Scheduled Tasks.

Rich

kim3er
That's not true. You can schedule just fine in ASP.NET code, as long as the application is running. In IIS 7.5, you can force apps to run as well, even if there are no requests being done.
Thorarin
You can force a web application to warm start, but I don't believe you can schedule specific code for within the application. If I am incorrect, I'll happily be proved wrong. Unfortunately, you haven't provided an extra information.
kim3er
@kim3er: You can start background threads in ASP.NET, like in any .NET application. From the background thread, you can do whatever background task or scheduling you like.
Thorarin
ASP .Net can fully utilise the Windows Workflow Foundation libraries which support long running processes independent of IIS and state.
Brian Scott
+2  A: 

I've often used wget / curl to do exactly this from windows scheduled tasks (or even from cron on a different server). For certain things it is better to keep it all within the web application.

e.g.

   curl -k  https://example.com/update.aspx?id=71077345 -u username:password

or

   wget -O - http://example.com/process.php 
Richard Harrison
Thanks dude, its very useful..
Neeraj
A: 

If you have full access to the server, use the scheduled tasks.

If you do not have access (shared hosting), create a webpage/service that you can call to trigger the action. Be aware that the "action" is limited in time.

You could create a small application, that you can schedule on you own computer to call the remote server, to trigger the action.

GvS
+1  A: 

ASP.Net has good integration with Windows Workflow (WF). I've accomplished 2 project with such integration. WF allows persists tasks, so it is independent on IIS restarting or system crash

Dewfy