Reputation: 19479
In my ASP.NET website, I am having a function which has to be automatically performed once every 2-3 mins on its own, i.e. without user intervention. This function contains database access.
Can I use threading to perform this process in background?
If yes, how can I use that? Any reference to tutorials would be really helpful.
Edit
Also I am not looking for a solution which uses Windows Services because I am using shared hosting. So I don't have all the rights to access the host computer.
Upvotes: 0
Views: 2278
Reputation: 5081
Create a little executable program that you can put on a client machine. Put that on the task scheduler to run every few minutes. When it runs, have it hit a webpage in your web app that does what you need to do.
The problem with what you're trying to do on the server is that if nobody hits the app for a while, it won't run. Since you don't have scheduling on the server, you're stuck with some very ugly hacks on the server or using scheduling somewhere else (like on a client).
Upvotes: 0
Reputation: 94653
I'd suggest to read interesting blog post by Jeff - Easy Background Tasks in ASP.NET and some stackoverflow threads:
Upvotes: 1
Reputation: 12326
You can in theory do this, but it is wrong in so many ways. First of all the threads must be started by someone visiting the page, second you have no warranty on how long the threads will live before some condition causes them to stop. Main thumb of rule: Don't use threads in ASP.Net.
The correct way of doing this is as @Matti mentions either by task scheduler or service. In addition if these tasks are SQL-related you may want to schedule a task on the SQL-server instead.
But... There is a quick-fix. When a user visits the page, first check how long since last maintenance and then simply perform the maintenance up-front if required. The user may get a few ms/seconds extra delay on the first request, but you don't have to mess up your solution with a separate windows service or scheduled task.
Upvotes: 0
Reputation: 65176
I'd imagine it would be easier and more efficient to do it using the task scheduler. Or perhaps even a service.
Upvotes: 5