Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am writing a Windows Service (using C# .NET 3.5 VS2008) and my requirement is:

  • When the Windows Service start - it performs record check operation (in Database) @ every 30 second interval (I have used a timer for this)

  • If the appropriate record was found in the database - then it starts multiple threads and performs the required operation (that may be network operation or Database operation).

The number of multiple threads also depends on the amount of records found (like if 10 record found then starts 5 threads and for 20 records start 10 threads etc...) How to design/code for these multiple threads?

Example: 10 records found in the database, I want to start 5 threads for first 5 records which may perform network operation... out of 5 threads whichever thread completes his job - will start processing for next the record (i.e Record number 6, 7 ... 10)

Please suggest best possible solution for the above case.

share|improve this question
This question is probably better for SO –  Pratik Jun 2 '11 at 8:06
@Pratik - I'm not so sure about that. There's not a specific coding issue. It's a design question which is on topic for here. –  ChrisF Jun 2 '11 at 8:33
His requirements cannot be meet. There are very few computers on the market that the capability to run 12 threads concurently. His design requirements are not realistic. –  Ramhound Jun 2 '11 at 12:26
@Ramhound: calling ThreadPool.GetMaxThreads() on my year old i3 desktop reports 1023 worker threads and 1000 completion port threads... Considering that my machine is also running 50+ processes at any given time, what do you mean that there are few computers that can run 12 threads concurrently? –  Steve Evers Jun 2 '11 at 13:11

3 Answers 3

I would recommend using Parallel.Net and PLINQ instead of explicit multi-threading. It's easier to manage, is more lightweight and will adapt itself to the numbers of core's you have

Learning resources


share|improve this answer

on service start, launch a thread to run your timer process

on service end, kill all open threads

beware that any unhandled exception in your process will silently kill the timer thread

throttle the number of open threads allowed using a private list, to avoid flooding the service

you might find my SafeThread class useful for this

share|improve this answer

Initial thoughts: too many threads are bad for you. Unless the thread are IO bound (ie stuck waiting on IO operations for a large amount of their time) then you should have 2 threads per CPU as a rough rule-of-thumb.

As a reuslt, create a thread pool of a fixed number of threads, and simply reuse them. The design of the pool requires a semaphore - set the semaphore count to be the number of threads in the pool, allow through as many requests as the semaphore counts up to, then the semaphore will block until the threads complete (when this happens, you decrement the semaphore count) which will allow the next request through. This works because a semaphore is simply a "multi-lock" mutex. It lets through as many calls as you've specified and then it blocks. A mutex is in fact a semaphore with a count of 1.

share|improve this answer
While I mostly agree, why would you suggest a custom thread pool instead of the .net thread pool? You can still use a semaphore for throttling. Also, do you have any reference material for the restriction of threads. For slightly long lived threads, I'm pretty sure >2 threads/cpu is not an issue but I could be completely wrong. –  Steve Evers Jun 2 '11 at 13:14
I am reading few documents and learning threading stuff a lot... I have question regarding Thread Pool. 1) Is Thread Pool is good option for executing multiple network operations (Http post operations)? 2) How Thread Pool executes each Thread (i.e If i assign 10 operation to Thread Pool - does it starts all 10 operations at the same time or it works linearly (one by one) ) Thanks for help and support. –  Ankit Shah Jun 3 '11 at 6:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.