I would suggest you hook up all your individual work items to a ManualResetEvent
then queue up all your work items in the ThreadPool
and then call WaitHandle.WaitAll(myResetEvents);
to wait until all of your work items are complete, then close your service down, etc.
Program Flow
- Start Service
- Start Listening
- Queue Work Items
- Stop Listening for new items
- Wait for all items to finish -WaitHandle.WaitAll(allMyQueuedWorkitems)
- Stop Service