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 Starts multiple threads

Windows Service Starts multiple threads

Scheduled Pinned Locked Moved C#
databaseperformancehelpquestion
4 Posts 3 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.
  • L Offline
    L Offline
    livez
    wrote on last edited by
    #1

    Hello! I´ve created an windows service with a timer-object. My application runs a method with a given interval, which builds up strings in memory and when finished with one string sends data to our database. Problem is, my application seems to be running on several threads, which messes up the string-values I hold in memory. I run this code in the start of the method I´ve set my timerobject to perform:

    Log.Write("current threadid : " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

    which gives me current threadid : 4 current threadid : 7 current threadid : 8 current threadid : 4 current threadid : 7 current threadid : 8 current threadid : 4 current threadid : 8 current threadid : 7 current threadid : 8 etc. Is this how windows services work, and can I manage this behaviour somehow?

    N N 2 Replies Last reply
    0
    • L livez

      Hello! I´ve created an windows service with a timer-object. My application runs a method with a given interval, which builds up strings in memory and when finished with one string sends data to our database. Problem is, my application seems to be running on several threads, which messes up the string-values I hold in memory. I run this code in the start of the method I´ve set my timerobject to perform:

      Log.Write("current threadid : " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

      which gives me current threadid : 4 current threadid : 7 current threadid : 8 current threadid : 4 current threadid : 7 current threadid : 8 current threadid : 4 current threadid : 8 current threadid : 7 current threadid : 8 etc. Is this how windows services work, and can I manage this behaviour somehow?

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      livez wrote:

      Problem is, my application seems to be running on several threads, which messes up the string-values I hold in memory.

      Problem will be with timer. You should not use a timer here. Try to run your method in a thread. You can also read Luc's article Timer surprises, and how to avoid them[^].

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • L livez

        Hello! I´ve created an windows service with a timer-object. My application runs a method with a given interval, which builds up strings in memory and when finished with one string sends data to our database. Problem is, my application seems to be running on several threads, which messes up the string-values I hold in memory. I run this code in the start of the method I´ve set my timerobject to perform:

        Log.Write("current threadid : " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

        which gives me current threadid : 4 current threadid : 7 current threadid : 8 current threadid : 4 current threadid : 7 current threadid : 8 current threadid : 4 current threadid : 8 current threadid : 7 current threadid : 8 etc. Is this how windows services work, and can I manage this behaviour somehow?

        N Offline
        N Offline
        Nicholas Butler
        wrote on last edited by
        #3

        I presume you are using System.Threading.Timer? When this fires, it uses a thread from the thread pool to run your TimerCallback method. You can verify this using:

        System.Threading.Thread.CurrentThread.IsThreadPoolThread

        If your method takes longer to execute than the timer period, then when the timer fires again, it will run your method again ( concurrently ) on a different thread pool thread. You have to take this into account when writing your TimerCallback method. Nick

        ---------------------------------- Be excellent to each other :)

        L 1 Reply Last reply
        0
        • N Nicholas Butler

          I presume you are using System.Threading.Timer? When this fires, it uses a thread from the thread pool to run your TimerCallback method. You can verify this using:

          System.Threading.Thread.CurrentThread.IsThreadPoolThread

          If your method takes longer to execute than the timer period, then when the timer fires again, it will run your method again ( concurrently ) on a different thread pool thread. You have to take this into account when writing your TimerCallback method. Nick

          ---------------------------------- Be excellent to each other :)

          L Offline
          L Offline
          livez
          wrote on last edited by
          #4

          Thanks alot guys, u were spot on!

          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