views:

245

answers:

2
    protected override void OnStart(String[] args)
    {
        ResultManager.PrepareCache();
        ThreadPool.QueueUserWorkItem(ResultQueue.Process);
        ThreadPool.QueueUserWorkItem(StatusUpdater.UpdateStatus);
        ThreadPool.QueueUserWorkItem(GeneralQueue.RestartHungTests);
        ThreadPool.QueueUserWorkItem(ResultManager.SyncroniseResultsTable);
        ThreadPool.QueueUserWorkItem(GeneralQueue.RecoverLostResults);
        ThreadPool.QueueUserWorkItem(BrowserTestStartInfo.FillQueues);
        ThreadPool.QueueUserWorkItem(MailAppAccount.FillQueues);
    }

Each of these tasks run for the duration of the life of the Windows Service. I've always stuck to the ThreadPool for this kind of thing, should I be just starting normal threads? Can I be certain the ThreadPool will have enough threads available to run each task? If I SetMaxThreads to 7, will I run into issues later because these threads never abort? Is it safe to set it to something much higher?


Edit:

I always want all 7 threads to be running simultaneously, and they never abort - should I even be using threads? Is there something else more suitable for this kind of perpetually running task?

Each task runs a particular method every x minutes.

+8  A: 

This is not an appropriate use of the thread pool. Just create normal threads, since they're long-lived. The overhead of creating the threads won't matter, since you'll only be creating them once.

John Saunders
Ah, I see, thanks!
Matthew Brindley
@Downvoter: look at my rep number. Two points means _nothing_ to me, and neither do you, since you can't be bothered to say what problem you have with this answer. If you want to matter to me, don't just downvote - downvote and say why.
John Saunders
+3  A: 

As John says, this is not a good idea. The reason is that the threadpool has a limited number of threads, and you are using a large number of them and never returning them. The threadpool is designed to pool thread usage for short lived threads.

You don't really need to manage the threads since they live the lifetime of the app.

Mystere Man
Thanks, that def makes more sense now :-)
Matthew Brindley