Use a Pool of Threads

Problem

You need to create an unbounded number of threads without degrading performance.

Solution

Use the ThreadPool class to map a large number of tasks to a fixed number of reusable threads.

Discussion

The System.Threading.ThreadPool class allows you to execute code using a pool of threads provided by the common language runtime. Using this pool simplifies your code, and it can improve performance, particularly if you use a large number of short-lived threads. The ThreadPool class avoids the overhead of continually creating and destroying threads by using a pool of reusable threads. You use the ThreadPool class to queue a task, and the task is processed on the first available thread. The ThreadPool class is capped at 25 threads ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.