tags:

views:

247

answers:

5

I used System.Timers.timer in global.as in asp.net to set a timer for scheduling execute a function let' say transferMoney(). But it seems that this timer might stop after several hours unexpected. And this cause that all the actions are pending. I want to know whether there are any better methods to set up a timer in asp.net, MVC 1.0? Thanks in advance!

A: 

You could try the system.threading.timer

http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

Shiraz Bhaiji
+2  A: 

Any timer you'll use in ASP.NET apps will eventually "terminate", but this a very expected behavior due to process recycling.

Anton Gogolev
so how can i avoid this ??? i googled it, and i know that System.timers.timer are used for server, but does it have any special feature?
MemoryLeak
+1  A: 

The timer will never work because IIS will reschedule the worker process regularly based on Application Pool settings, so when it recycles your timer will get destroyed and you might need to reopen it.

You can put a check on whether timer object is still available or not, if not available then create it !!, using any other timer object will not work. But this still has a problem, because if you dont have any web request for particular period of time, it will still get destroyed. Best is to setup a ping monitor from other place which can keep your website alive.

Akash Kava
+4  A: 

It might just be because the application got recycled. Global.ashx is not really the right place to do long running tasks because if your AppDomain gets recycled your timer will die. I suggest making a job windows service instead.

Edit: Well, it's fairly easy to create a windows service project in Visual Studio just do [File] > [Add] > [New Project...] > [Windows] > [Windows Service] and you will get the stub code for the project.

It's hard to come up with a complete example so i suggest you google it. ;) There are tons of samples out there for you to look at.

This article on CodeProject seems to be a good introduction to Windows Services.

JohannesH
How to handle it ?could you please give me some sample code ?
MemoryLeak
As he states running another program (such as a windows service), the problem is basically that your app (website) is being shutdown every so often by the servers recycle policy.. if you want long running tasks you are better using a windows service.
meandmycode
+1  A: 

You can't reliably run a timer in ASP.NET. If there are no requests coming in, the IIS can shut down the application, and it will not start until the next request arrives.

Why do you think that you need a timer? In most web applications this is not needed at all to do periodical updates unless they depend on an external source.

If you are just moving data around inside your application, the actual transactions doesn't have to happen at an exact interval, you only have to calculate what the result would be if they had happened. Whenever a request comes in, you calculate how many transactions would have happened since the last request, and do them to catch up to the current state.

If your transactions rely on an external source so that they actually has to run at a specific time, you simply can't do it with ASP.NET alone. You need an application that runs outside IIS, for example started periodically by the windows scheduler.

Guffa