Windows Service Starts multiple threads
-
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? -
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?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
-
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?I presume you are using
System.Threading.Timer
? When this fires, it uses a thread from the thread pool to run yourTimerCallback
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 :)
-
I presume you are using
System.Threading.Timer
? When this fires, it uses a thread from the thread pool to run yourTimerCallback
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 :)