MemoryLeak
MemoryLeak

Reputation: 7318

any better timer in asp.net?

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!

Upvotes: 0

Views: 785

Answers (5)

JohannesH
JohannesH

Reputation: 6450

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.

Upvotes: 4

Guffa
Guffa

Reputation: 700372

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.

Upvotes: 1

Akash Kava
Akash Kava

Reputation: 39916

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.

Upvotes: 1

Anton Gogolev
Anton Gogolev

Reputation: 115749

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

Upvotes: 2

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65391

You could try the system.threading.timer

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

Upvotes: 0

Related Questions