Periodically run an event on Server side itself
I want to run an event periodically on the server side that is to be initiated by the server 开发者_JS百科in ASP.NET scenario. There will be no request from client.
Your help will be much appreciated.
Assuming you have access to the server and can install external applications, you could create a stand-alone Windows Service or even a console application that is triggered by the built in Windows Task Scheduler.
For something that runs in Asp.Net you could use Quartz.net. This SO question explains how to use it with Asp.Net
You need to spawn a process using the Process class. The process will sleep until the recurrence time comes.
Process p = new Process();
p.StartInfo.FileName = "foo";
p.Start();
Another alternative is to spool a thread using the Thread class.
For a similar situation in some of the applications, I use a separate service (windows service/web service) and use System.Threading.Timer
(see http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx) and implement a scheduler of my own. Or otherwise, you can use any server based standard scheduler to invoke some executable that in turn hits your service.
I would just create a standard command line executable and have it run with the Windows Scheduling Tool. A windows service is fine if it runs in very short intervals, but for example for once a day task, I think its the wrong approach.
Have a look at the Windows Workflow Foundation
That framework is built for this kind of things, like state persistance etc. It also runs separate, so the process will not go down when your app(pool) goes down (by default due to IIS).
Integrates fully with asp.net and you'll probably end up (in a positive way) with a Delay Activity.
精彩评论