Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Issue with the Windows Service OnTimeElapsed

Issue with the Windows Service OnTimeElapsed

Scheduled Pinned Locked Moved C#
helpdatabasequestion
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpavas
    wrote on last edited by
    #1

    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

    G realJSOPR 2 Replies Last reply
    0
    • M mpavas

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • G Guffa

        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.

        M Offline
        M Offline
        mpavas
        wrote on last edited by
        #3

        So when it will again start?

        Regards, Pavas

        M G 2 Replies Last reply
        0
        • M mpavas

          So when it will again start?

          Regards, Pavas

          M Offline
          M Offline
          mpavas
          wrote on last edited by
          #4

          I believe, timer will start right after the method call is over..let me try this

          Regards, Pavas

          1 Reply Last reply
          0
          • M mpavas

            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

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            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

            S 1 Reply Last reply
            0
            • M mpavas

              So when it will again start?

              Regards, Pavas

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              It will start when you turn the timer on again.

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • realJSOPR realJSOP

                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

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                John 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 or System.Timers.Timer. AFAIK, both of them use the ThreadPool, and so does BackgroundWorker

                Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups