Issue with the Windows Service OnTimeElapsed
-
Hello, I have a below issue with the windows service. I have a method as below private void OnElapsedTime(object source, ElapsedEventArgs e) { log.Info("OnElapsedTime"); BillPay.ProcessOrder(); } I have a timer set for 1 mins in OnStart method protected override void OnStart(string[] args) { //ad 1: handle Elapsed event timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); //ad 2: set interval to 1 minute (= 60,000 milliseconds) timer.Interval = 60000 ; //ad 3: enabling the timer timer.Enabled = true; } The problem: If my ProcessOrder() method which process all orders do not get completed in 1 mins I see unexpected results in the database with duplicate entry. I suspect the method ProcessOrder() get called again onTimeElapsed, I have commit and rollback statements too. But does not help. Please suggest as how can I resolve this issue.
Regards, Pavas
-
Hello, I have a below issue with the windows service. I have a method as below private void OnElapsedTime(object source, ElapsedEventArgs e) { log.Info("OnElapsedTime"); BillPay.ProcessOrder(); } I have a timer set for 1 mins in OnStart method protected override void OnStart(string[] args) { //ad 1: handle Elapsed event timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); //ad 2: set interval to 1 minute (= 60,000 milliseconds) timer.Interval = 60000 ; //ad 3: enabling the timer timer.Enabled = true; } The problem: If my ProcessOrder() method which process all orders do not get completed in 1 mins I see unexpected results in the database with duplicate entry. I suspect the method ProcessOrder() get called again onTimeElapsed, I have commit and rollback statements too. But does not help. Please suggest as how can I resolve this issue.
Regards, Pavas
-
Stop the timer while you call the method, so that it does not start the event handler in another thread.
Despite everything, the person most likely to be fooling you next is yourself.
-
Hello, I have a below issue with the windows service. I have a method as below private void OnElapsedTime(object source, ElapsedEventArgs e) { log.Info("OnElapsedTime"); BillPay.ProcessOrder(); } I have a timer set for 1 mins in OnStart method protected override void OnStart(string[] args) { //ad 1: handle Elapsed event timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); //ad 2: set interval to 1 minute (= 60,000 milliseconds) timer.Interval = 60000 ; //ad 3: enabling the timer timer.Enabled = true; } The problem: If my ProcessOrder() method which process all orders do not get completed in 1 mins I see unexpected results in the database with duplicate entry. I suspect the method ProcessOrder() get called again onTimeElapsed, I have commit and rollback statements too. But does not help. Please suggest as how can I resolve this issue.
Regards, Pavas
Put your work into a thread, and fire off the thread each time the timer ticks. You can even prevent multiple threads from kicking off with appropriate code in the tick handler. Personally, I wouldn't use a Timer object because it's the lowest priority message in Windows, and in a busy system, there's no guarantee the message will even be handled. Instead, I use a
BackgroundWorker
object for timers because they can be easily aborted. Sure, it's more code (go forbid a programmer should have to actually write some code), but it's much more reliable and controllable."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Put your work into a thread, and fire off the thread each time the timer ticks. You can even prevent multiple threads from kicking off with appropriate code in the tick handler. Personally, I wouldn't use a Timer object because it's the lowest priority message in Windows, and in a busy system, there's no guarantee the message will even be handled. Instead, I use a
BackgroundWorker
object for timers because they can be easily aborted. Sure, it's more code (go forbid a programmer should have to actually write some code), but it's much more reliable and controllable."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
Personally, I wouldn't use a Timer object because it's the lowest priority message in Windows, and in a busy system, there's no guarantee the message will even be handled. Instead, I use a BackgroundWorker
That is true for a Windows Forms timer running on the UI thread. But the OP is writing a service, so he's probably using
System.Threading.Timer
orSystem.Timers.Timer
. AFAIK, both of them use the ThreadPool, and so doesBackgroundWorker
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro