Is it true that for long running processes it is better to do thread manually instead of threadpool?

user1193665 picture user1193665 · Apr 24, 2012 · Viewed 11.4k times · Source

I read on the other day that for long-running tasks my best bet is to manually create threads instead of using .NET’s thread pool or Task Parallel. I'd really like someone to enlighten me as I am learning about c# threading, specially for long running IO tasks. Thank you in advance.

Answer

Nicholas Butler picture Nicholas Butler · Apr 24, 2012

That is true. The thread pool is optimised for small units of work and you can interfere with other work by holding onto a thread pool thread.

My rule of thumb is if an operation can take more than a second, it should not be on a thread pool thread. That is probably quite long.

Although this is undocumented, if you start a Task with TaskCreationOptions.LongRunning then a new Thread will be started to run the Task.

For most IO tasks, there are asynchronous versions of the framework methods that you should really use. These make use of kernel functions and mean that you won't be blocking any thread.

As always, I recommend reading Joe Albahari's free ebook, followed by Joe Duffy's Concurrent Programming on Windows. The later is 1000 pages long, but is full of useful details.