The ThreadPool class provides your application with a pool of worker threads that are managed by the system, allowing you to concentrate on application tasks rather than thread management. If you have short tasks that require background processing, the managed thread pool is an easy way to take advantage of multiple threads.
ThreadPool 类提供给应用一个worker 线程的pool,允许你更关注应用task而不是线程管理。
For background tasks that interact with the user interface, the .NET Framework version 2.0 also provides the BackgroundWorker class, which communicates using events raised on the user interface thread.
The .NET Framework uses thread pool threads for many purposes, including asynchronous I/O completion, timer callbacks, registered wait operations, asynchronous method calls using delegates, and System.Net socket connections.
When Not to Use Thread Pool Threads
--------------------------------------------------------------------------------
There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads:
以下情况不合适使用线程池线程。
•You require a foreground thread. 需要使用前台线程【如果前台线程还在运行,系统就不会停止】
•You require a thread to have a particular priority. 需要有特定优先级的线程
•You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting.
有会长时间block线程的task。线程池是有数量限制的,如果有大量的blocked线程池线程会组织task启动。
•You need to place threads into a single-threaded apartment. All ThreadPool threads are in the multithreaded apartment.
需要将线程放到一个single-threaded apartment。所有ThreadPool 中的线程都在一个多线程的apartment中。
•You need to have a stable identity associated with the thread, or to dedicate a thread to a task.
需要有一个不变的 ID与线程关联,或者将某个线程只用于某一个task。
A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.
后台线程不会使得托管执行环境继续运行。如果一个进程的所有的前台线程都停止了,那么系统会停止所有的后台线程并且shutdown运行环境。