Windows Service with Timer Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan

    Windows Service with Timer Question

    Hello everyone,

    I have made a service that starts timers when it starts. I have
    another windows form application that stops and starts the service. Do
    I need to deal with any started timers when the service stops? Being
    new at this service stuff, I don't know if it holds state or memory
    and I need to stop all active timers or if the magical gc will come
    along and help out.

    Thanks, I hope that makes sense.

    Have a great day,
    Ryan

  • DArnold

    #2
    Re: Windows Service with Timer Question

    Ryan wrote:
    Hello everyone,
    >
    I have made a service that starts timers when it starts. I have
    another windows form application that stops and starts the service. Do
    I need to deal with any started timers when the service stops? Being
    new at this service stuff, I don't know if it holds state or memory
    and I need to stop all active timers or if the magical gc will come
    along and help out.
    >
    Thanks, I hope that makes sense.
    >
    In your case here, if the timers were started on the parent thread, the
    tread the service started up on, the the timers are going to be killed
    when the service stops. However, if the timer was instantiated on a
    spawned child thread and you don't kill the thread even though you have
    stopped the service, then the child thread and the timer will still be
    running.

    The magical GC will kill the timer while the service is running if
    you're not careful, GC.Keepalive will prevent that.

    The proper way to write a timed execution in a .Net service is to use
    threads on a timed basis, using the Tread.Sleep in a While True Loop.





    Comment

    • Smokey Grindle

      #3
      Re: Windows Service with Timer Question

      I have NEVER heard that.... I was always told by people and MS people to use
      the Threading.Timer for services and to always avoid while loops for this

      "DArnold" <DArnold@DArnol d.comwrote in message
      news:eRXoszrrHH A.404@TK2MSFTNG P06.phx.gbl...
      Ryan wrote:
      >Hello everyone,
      >>
      >I have made a service that starts timers when it starts. I have
      >another windows form application that stops and starts the service. Do
      >I need to deal with any started timers when the service stops? Being
      >new at this service stuff, I don't know if it holds state or memory
      >and I need to stop all active timers or if the magical gc will come
      >along and help out.
      >>
      >Thanks, I hope that makes sense.
      >>
      >
      In your case here, if the timers were started on the parent thread, the
      tread the service started up on, the the timers are going to be killed
      when the service stops. However, if the timer was instantiated on a
      spawned child thread and you don't kill the thread even though you have
      stopped the service, then the child thread and the timer will still be
      running.
      >
      The magical GC will kill the timer while the service is running if you're
      not careful, GC.Keepalive will prevent that.
      >
      The proper way to write a timed execution in a .Net service is to use
      threads on a timed basis, using the Tread.Sleep in a While True Loop.
      >
      >
      >
      >
      >

      Comment

      • Mr. Arnold

        #4
        Re: Windows Service with Timer Question


        "Smokey Grindle" <nospam@nospam. comwrote in message
        news:%23yE%23n7 rrHHA.192@TK2MS FTNGP02.phx.gbl ...
        >I have NEVER heard that.... I was always told by people and MS people to
        >use the Threading.Timer for services and to always avoid while loops for
        >this
        What MS people? When I first started writing .Net services on VS 2003 using
        the Framework 1.1, a guy I worked for questioned me about why I was using a
        While True loop. I told him it came right out of the VS Help for threading.

        Then he came back with an article about the Whlie True loop was the
        preferred method using Thread.Sleep in a .NET Service. I have been looking
        for that link, but I cannot find it.

        When I am writing a .NET Service or Console application, then I am going to
        spawn a child thread using a delegate with a While True loop, as the worker
        thread or threads. The only Timer I will use is to check thread state.

        public class StartThread()
        {

        try
        {
        while true
        {
        do something
        Thread.Sleep(ti metowait)
        }
        }
        Catch ThreadException as tex //if it hits the Catch, then it's coming
        out of the loop
        {
        do something
        }
        }


        Comment

        • Harry Strybos

          #5
          Re: Windows Service with Timer Question

          "Smokey Grindle" <nospam@nospam. comwrote in message
          news:%23yE%23n7 rrHHA.192@TK2MS FTNGP02.phx.gbl ...
          >I have NEVER heard that.... I was always told by people and MS people to
          >use the Threading.Timer for services and to always avoid while loops for
          >this
          >
          "DArnold" <DArnold@DArnol d.comwrote in message
          news:eRXoszrrHH A.404@TK2MSFTNG P06.phx.gbl...
          >Ryan wrote:
          >>Hello everyone,
          >>>
          >>I have made a service that starts timers when it starts. I have
          >>another windows form application that stops and starts the service. Do
          >>I need to deal with any started timers when the service stops? Being
          >>new at this service stuff, I don't know if it holds state or memory
          >>and I need to stop all active timers or if the magical gc will come
          >>along and help out.
          >>>
          >>Thanks, I hope that makes sense.
          >>>
          >>
          >In your case here, if the timers were started on the parent thread, the
          >tread the service started up on, the the timers are going to be killed
          >when the service stops. However, if the timer was instantiated on a
          >spawned child thread and you don't kill the thread even though you have
          >stopped the service, then the child thread and the timer will still be
          >running.
          >>
          >The magical GC will kill the timer while the service is running if you're
          >not careful, GC.Keepalive will prevent that.
          >>
          >The proper way to write a timed execution in a .Net service is to use
          >threads on a timed basis, using the Tread.Sleep in a While True Loop.
          >>
          >>
          >>
          >>
          >>
          >
          have a look at



          Comment

          • Michel Posseth  [MCP]

            #6
            Re: Windows Service with Timer Question

            I concur with smokey about this

            i also remember that the way to go in services is to use a threading timer

            i have a few services out there running with a threading timer without anny
            problems

            michel posseth



            "Smokey Grindle" <nospam@nospam. comschreef in bericht
            news:%23yE%23n7 rrHHA.192@TK2MS FTNGP02.phx.gbl ...
            >I have NEVER heard that.... I was always told by people and MS people to
            >use the Threading.Timer for services and to always avoid while loops for
            >this
            >
            "DArnold" <DArnold@DArnol d.comwrote in message
            news:eRXoszrrHH A.404@TK2MSFTNG P06.phx.gbl...
            >Ryan wrote:
            >>Hello everyone,
            >>>
            >>I have made a service that starts timers when it starts. I have
            >>another windows form application that stops and starts the service. Do
            >>I need to deal with any started timers when the service stops? Being
            >>new at this service stuff, I don't know if it holds state or memory
            >>and I need to stop all active timers or if the magical gc will come
            >>along and help out.
            >>>
            >>Thanks, I hope that makes sense.
            >>>
            >>
            >In your case here, if the timers were started on the parent thread, the
            >tread the service started up on, the the timers are going to be killed
            >when the service stops. However, if the timer was instantiated on a
            >spawned child thread and you don't kill the thread even though you have
            >stopped the service, then the child thread and the timer will still be
            >running.
            >>
            >The magical GC will kill the timer while the service is running if you're
            >not careful, GC.Keepalive will prevent that.
            >>
            >The proper way to write a timed execution in a .Net service is to use
            >threads on a timed basis, using the Tread.Sleep in a While True Loop.
            >>
            >>
            >>
            >>
            >>
            >
            >

            Comment

            • Mr. Arnold

              #7
              Re: Windows Service with Timer Question


              "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
              news:OTaVrzLsHH A.1864@TK2MSFTN GP04.phx.gbl...
              >I concur with smokey about this
              >
              i also remember that the way to go in services is to use a threading timer
              >
              i have a few services out there running with a threading timer without
              anny problems
              >
              What? You think I am making this up?

              I concur with what the VS 2003 Help talked about in its examples of using a
              While True loop with Thread.Sleep. I also concur with the article that was
              placed before me about what should be done in a timed execution of a thread
              in a .NET Service application, which was to use a While True loop and
              Thread.sleep. But I can't find that article, which was presented to me back
              in 2004. I recall it was a MSDN article.

              I have written a few .Net service and Console applications myself, and the
              method I use for Treading using a sleep/wait period is what I talked about,
              without any problems. The only Timer I use is to check Thread or Threads
              state on a periodic basis.

              ..Net MCP 2003 vantage as well here and maybe MCPD 2005 vantage if I have the
              energy to do it, as I am getting too old and may not go after it. But I will
              go through the MS Press training kit books anyway.

              Comment

              • Mr. Arnold

                #8
                Re: Windows Service with Timer Question

                One other thing here, MS MCTS 70-536 book has an entire chapter on
                Threading. Throughout the entire chapter 7 with its examples of execution of
                a thread on a timed basis, it's using a For or While loop with Tread.Sleep.
                There is only one page that talks about using a System.Thread.T imer, which I
                cannot say not to use a Timer. But the preferred way it seems, at least in
                the examples of Threading in the book, are using a loop with Thread.Sleep.

                Comment

                • Smokey Grindle

                  #9
                  Re: Windows Service with Timer Question

                  every MS person I ever talked to said use threading.timer s... I am talking
                  about in person here at tech demos and such... they always shyed away from
                  using while loops and sleeping

                  "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
                  news:OlrmR1MsHH A.4888@TK2MSFTN GP03.phx.gbl...
                  One other thing here, MS MCTS 70-536 book has an entire chapter on
                  Threading. Throughout the entire chapter 7 with its examples of execution
                  of a thread on a timed basis, it's using a For or While loop with
                  Tread.Sleep. There is only one page that talks about using a
                  System.Thread.T imer, which I cannot say not to use a Timer. But the
                  preferred way it seems, at least in the examples of Threading in the book,
                  are using a loop with Thread.Sleep.

                  Comment

                  • Mr. Arnold

                    #10
                    Re: Windows Service with Timer Question


                    "Smokey Grindle" <nospam@nospam. comwrote in message
                    news:OTdsNWbsHH A.1168@TK2MSFTN GP02.phx.gbl...
                    every MS person I ever talked to said use threading.timer s... I am talking
                    about in person here at tech demos and such... they always shyed away from
                    using while loops and sleeping
                    That's them and I don't agree with them is the bottom line.

                    Comment

                    • Phill W.

                      #11
                      Re: Windows Service with Timer Question

                      Smokey Grindle wrote:
                      every MS person I ever talked to said use threading.timer s... I am talking
                      about in person here at tech demos and such... they always shyed away from
                      using while loops and sleeping
                      IMHO, there's one very Good Reason for /not/ using a Timer.

                      It's called JIT compilation.

                      You write a Service and put a Timer in it.
                      You code up the routine that the Timer calls.
                      This routine references an external Assembly.

                      You deploy the Service.
                      Somehow, you miss the dependent assembly.

                      Your deployed service runs!
                      It starts and stops perfectly!
                      It doesn't report /any/ errors or Exceptions!

                      But it never does any useful work.

                      Why???

                      When the Timer fires, the runtime attempts to JIT the method invoked by
                      the Timer.
                      With the referenced Assembly /missing/, this JIT-linking fails but the
                      runtime doesn't report this and, if the Exception gets logged anywhere,
                      I've /never/ managed to find it. You can't catch this Exception - it's
                      gets thrown into the depths of the runtime and the Service
                      Infrastructure - you never see it.

                      I've found that calling the "worker" method from another one inside the
                      service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
                      even get to catch the Exception if you really want to. ;-)

                      Regards,
                      Phill W.

                      Comment

                      Working...