C#: Window Service with thread pool

Hello Folks,

FYI, actually, I need to send emails from multiple databases (schema is same) then, I would like to use thread pool.
I would like to create a window service in C# which will be having below features:

1. I would like to use Thread Pool in window service;
2. The threads will be configurable like 2 threads.
3. The chunk size will be configurable like 5 i.e. records chunk size.
4. Could you please provide me code snippet for this?
a.) Aforesaid 1 to 3 points
b.) I need to put logic to utilize threads once idle. For example, two threads (using Thread Pool) are running in window service;
c.) And 500 emails need to send;
d.) Pre-assume, we have set 2 threads and 5 records chunk size; That means, 10 records records are processing in two threads
e.) So, window service logic should not wait to complete 10 records. In case one of the thread is free then assign next 5 records chunk size.

Best Regards,
Mohit Pandit
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
then you need to enable a FIFO queue
it rarely makes sense to part emails in chunks which would be sent by different threads. consider the case where the first thread was delayed for some reason and the second thread already sends next packet.

so each thread from threadpool should sent at least one complete mail before exiting.

sample code for thread-pool you could take from


here the threads were invoked in for loop which you would replace by an own for loop which iterates all mail jobs.

if you need code how to create a windows service (c#) you may look to



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MohitPanditAuthor Commented:
Hi Sara,

Sure, I'll take a look over it and update you.

Best Regards
MohitPanditAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.