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. Windows Service: Timer vs Thread

Windows Service: Timer vs Thread

Scheduled Pinned Locked Moved C#
discussionvisual-studioperformancequestion
9 Posts 5 Posters 0 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.
  • B Offline
    B Offline
    BBatts
    wrote on last edited by
    #1

    Hello All, I was wanting to start a discussion about best practice when creating threads in a Windows Service. I have a service that has multiple process running at the same time (6-10). Is it better to use timers or threads? I personally prefer threads because I can end them all with a single line of code where as if have to end each timer individually. Plus with threads, you don't have to worry about it calling it self until you want it to. Does anybody have any other thoughts on this? What's a better way to go and why? Which is lighter on PC usage and memory? etc etc Thanks in advance, Bryan

    J L T 3 Replies Last reply
    0
    • B BBatts

      Hello All, I was wanting to start a discussion about best practice when creating threads in a Windows Service. I have a service that has multiple process running at the same time (6-10). Is it better to use timers or threads? I personally prefer threads because I can end them all with a single line of code where as if have to end each timer individually. Plus with threads, you don't have to worry about it calling it self until you want it to. Does anybody have any other thoughts on this? What's a better way to go and why? Which is lighter on PC usage and memory? etc etc Thanks in advance, Bryan

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      BBatts wrote:

      Is it better to use timers or threads?

      When the timer fires it uses a thread.

      BBatts wrote:

      Plus with threads, you don't have to worry about it calling it self until you want it to.

      Huh? If the thread is running and not blocked then it is fact running. If you weren't saying that I am not sure what you are saying because the execution flow is always in one thread or another.

      BBatts wrote:

      because I can end them all with a single line of code

      Not sure what that means exactly. At least sometimes one can't just end threads arbitrarily and sometimes one must end them in a certain order. But the same applies to timers but only to the extent that they too are using threads.

      B 1 Reply Last reply
      0
      • J jschell

        BBatts wrote:

        Is it better to use timers or threads?

        When the timer fires it uses a thread.

        BBatts wrote:

        Plus with threads, you don't have to worry about it calling it self until you want it to.

        Huh? If the thread is running and not blocked then it is fact running. If you weren't saying that I am not sure what you are saying because the execution flow is always in one thread or another.

        BBatts wrote:

        because I can end them all with a single line of code

        Not sure what that means exactly. At least sometimes one can't just end threads arbitrarily and sometimes one must end them in a certain order. But the same applies to timers but only to the extent that they too are using threads.

        B Offline
        B Offline
        BBatts
        wrote on last edited by
        #3

        "When the timer fires it uses a thread." Yes. To be more specific a timer is an object the fires a thread periodically. "Huh? If the thread is running and not blocked then it is fact running" If the thread your timer has fired takes longer to execute than the interval level of a timer it will fire again. Unless, of course, you disable it. Where as if one is using a System.Thread there is more direct control over when and how the thread is fired. I can't think of any reason to use a Timer as opposed to System.Thread...in a windows service. I guess that was the point of my post.

        J 1 Reply Last reply
        0
        • B BBatts

          Hello All, I was wanting to start a discussion about best practice when creating threads in a Windows Service. I have a service that has multiple process running at the same time (6-10). Is it better to use timers or threads? I personally prefer threads because I can end them all with a single line of code where as if have to end each timer individually. Plus with threads, you don't have to worry about it calling it self until you want it to. Does anybody have any other thoughts on this? What's a better way to go and why? Which is lighter on PC usage and memory? etc etc Thanks in advance, Bryan

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You are not comparing like with like: timers have one purpose, threads another.

          Use the best guess

          1 Reply Last reply
          0
          • B BBatts

            Hello All, I was wanting to start a discussion about best practice when creating threads in a Windows Service. I have a service that has multiple process running at the same time (6-10). Is it better to use timers or threads? I personally prefer threads because I can end them all with a single line of code where as if have to end each timer individually. Plus with threads, you don't have to worry about it calling it self until you want it to. Does anybody have any other thoughts on this? What's a better way to go and why? Which is lighter on PC usage and memory? etc etc Thanks in advance, Bryan

            T Offline
            T Offline
            tasumisra
            wrote on last edited by
            #5

            Quote:

            I have a service that has multiple process running at the same time (6-10)

            is it multiple thread or process..??and what are you trying to achieve with timer or thread..? can you please be bit more specific..

            vikas da

            B 1 Reply Last reply
            0
            • T tasumisra

              Quote:

              I have a service that has multiple process running at the same time (6-10)

              is it multiple thread or process..??and what are you trying to achieve with timer or thread..? can you please be bit more specific..

              vikas da

              B Offline
              B Offline
              BBatts
              wrote on last edited by
              #6

              Yes. It performs varies disk related services. Clean Up, Archive, Backup, etc etc. There can be up to 6 process running at the same time. I currently create and fire 6 System.Threads on the Start() event. Each thread has a loop that keeps it running with a Thread.Delay, so they look something like this:

              private void WorkerThread_DoWork()
              {
              while (contiueWorking)
              {
              //Perform Some Task

              System.Threading.Thread.Sleep(10000);
              

              }
              }

              The task may or may not take longer than 10 seconds to complete, depending on the amount of data that is being managed. So, my question is (and admittedly I'm stumbling trying to ask it) would it be better to have 6 timers in this situation or should I stick with the threads? Or am I simply overthinking it.

              N 1 Reply Last reply
              0
              • B BBatts

                Yes. It performs varies disk related services. Clean Up, Archive, Backup, etc etc. There can be up to 6 process running at the same time. I currently create and fire 6 System.Threads on the Start() event. Each thread has a loop that keeps it running with a Thread.Delay, so they look something like this:

                private void WorkerThread_DoWork()
                {
                while (contiueWorking)
                {
                //Perform Some Task

                System.Threading.Thread.Sleep(10000);
                

                }
                }

                The task may or may not take longer than 10 seconds to complete, depending on the amount of data that is being managed. So, my question is (and admittedly I'm stumbling trying to ask it) would it be better to have 6 timers in this situation or should I stick with the threads? Or am I simply overthinking it.

                N Offline
                N Offline
                Nicholas Marty
                wrote on last edited by
                #7

                I'd be using Timers for this purpose. As it's by far easier to control the execution of the timer instead of a thread. I generally use the System.Threading.Timer with the following callback pattern: Initialize it like that:

                // Declaration:
                System.Threading.Timer _timer;

                //Initialization:
                _timer = new System.Threading.Timer(OnTimer, null, 10000, System.Threading.Timeout.Infinite);

                And this callback method

                private void OnTimer(object state) {
                try {
                // Do your stuff here
                } catch (Exception ex) {
                // Errorhandling
                } finally {
                // the first parameter is the delay until the callback method will be called again
                // the second parameter defines the interval in which it will be rais. Timeout.Infition -> Disabled
                _timer.Change(10000, System.Threading.Timeout.Infinite);
                }
                }

                To stop the timer just call:

                _timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);

                If you use it like that it should never ever be called concurrently as after the first event the timer is disabled until you manually reenable it (i.e. in the finally clause) Greetings

                B 1 Reply Last reply
                0
                • N Nicholas Marty

                  I'd be using Timers for this purpose. As it's by far easier to control the execution of the timer instead of a thread. I generally use the System.Threading.Timer with the following callback pattern: Initialize it like that:

                  // Declaration:
                  System.Threading.Timer _timer;

                  //Initialization:
                  _timer = new System.Threading.Timer(OnTimer, null, 10000, System.Threading.Timeout.Infinite);

                  And this callback method

                  private void OnTimer(object state) {
                  try {
                  // Do your stuff here
                  } catch (Exception ex) {
                  // Errorhandling
                  } finally {
                  // the first parameter is the delay until the callback method will be called again
                  // the second parameter defines the interval in which it will be rais. Timeout.Infition -> Disabled
                  _timer.Change(10000, System.Threading.Timeout.Infinite);
                  }
                  }

                  To stop the timer just call:

                  _timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);

                  If you use it like that it should never ever be called concurrently as after the first event the timer is disabled until you manually reenable it (i.e. in the finally clause) Greetings

                  B Offline
                  B Offline
                  BBatts
                  wrote on last edited by
                  #8

                  Thanks Nicholas, I've never seen it done that way!

                  1 Reply Last reply
                  0
                  • B BBatts

                    "When the timer fires it uses a thread." Yes. To be more specific a timer is an object the fires a thread periodically. "Huh? If the thread is running and not blocked then it is fact running" If the thread your timer has fired takes longer to execute than the interval level of a timer it will fire again. Unless, of course, you disable it. Where as if one is using a System.Thread there is more direct control over when and how the thread is fired. I can't think of any reason to use a Timer as opposed to System.Thread...in a windows service. I guess that was the point of my post.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    BBatts wrote:

                    System.Thread there is more direct control over when and how the thread is fired.

                    How exactly do you "fire" a thread in the above? How exactly do you think that process is different than what a timer does?

                    BBatts wrote:

                    I can't think of any reason to use a Timer as opposed to System.Thread...in a windows service

                    Because one wants an action to occur based on something associated with time. For example running a process every hour or once a day.

                    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