0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

5 Answers5

5

I'd imagine it would be easier and more efficient to do it using the task scheduler. Or perhaps even a service.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
1

Take a look at this: How to use Quartz.net with ASP.NET

Community
  • 1
  • 1
Ron Klein
  • 9,178
  • 9
  • 55
  • 88
  • (IMHO) "Just because you can doesn't mean you should." ... You could just as easily hook up a thread start in your ASPX-solution. The problem is that it breaks with convention. I'd hate to be the sysadmin that maintains a project where programmers do stuff like this. :) – Tedd Hansen Jan 29 '11 at 13:06
  • I have edited my question. Check the `EDIT` portion of my Question – Parth Bhatt Jan 29 '11 at 13:33
1

I'd suggest to read interesting blog post by Jeff - Easy Background Tasks in ASP.NET and some stackoverflow threads:

  1. To create a worker thread and keep it alive throughout my application life time to perform some back ground tasks
  2. Best way to run a background task in ASP.Net web app and also get feedback ?
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

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.

Tedd Hansen
  • 12,074
  • 14
  • 61
  • 97
0

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).

Tridus
  • 5,021
  • 1
  • 19
  • 19