Microsoft .net framework provides various options to seek concurrency with help of asynchronous operations and parallel processing capabilities. There are 3 main approaches to deal with concurrency-
- Direct access to Thread
- Using ThreadPool
- Task based programming
Direct access of Thread is the most low level approach and tightly close to operating system rather than .net CLR management. It requires heavy attention to manage but in returns it gives more controlling options to developers. Since, these are the main threads at Operating System level, it is loaded with it's own stack and kernel resources and may be more costlier if not managed efficiently. These threads can be managed & controlled by setting thread stack size, apartment state, observation of thread state, setting culture, even a thread can be Abort, Suspend and Resume.
To provide more easiness by reducing thread managing & controlling burden from developers head, Microsoft introduced a great concept of ThreadPool. ThreadPool is managed by CLR and is a wrapper around a pool of threads. This is completely under control of CLR and doesn't give you more control as preceding. The size of ThreadPool can be optionally set and given job work is executed on it. CLR finds a free thread from the thread pool and get done the job work. ThreadPool itself doesn't intimate or return result when a job item is finished and a good option for small job items or those job items which are not likely to return result value.
One step further, Microsoft launched Task Parallel Library(TPL) which mixes features of Thread(notification) & ThreadPool (auto management of threads). Task uses a TaskScheduler which schedules the job work and works on ThreadPool. It is the most advanced and managed approach to achieve concurrency and has following features -
There is Microsoft's BCL library which provides Parallel processing capability through Parallel.For* , PLINQ which makes concurrent mechanism more approachable and easy.
- Task continuation option (ContinueWith() )
- Task<T> - a generic return type used for passing results
- Wait() option features for synchronous job work and used to for waiting of result
- Facility to spin long running job-work on new Thread rather than ThreadPool