What is the Task Parallel Library (TPL) in C#?
Answer
The Task Parallel Library (TPL) is a set of APIs in the System.Threading.Tasks namespace that simplifies parallel and asynchronous programming. Key components: Task: represents an asynchronous operation. Task<T>: returns a value. Task.Run(): queues work to the ThreadPool. Task.WhenAll(): await multiple tasks in parallel. Task.WhenAny(): complete when first task finishes. Parallel.For/ForEach: data parallelism with automatic partitioning across cores: Parallel.ForEach(items, item => Process(item));. PLINQ: parallel LINQ — items.AsParallel().Where(x => Expensive(x)).ToList(). CancellationToken: cooperative cancellation — pass to async methods and check in loops. IProgress<T>: report progress back to the UI thread. SemaphoreSlim: async-friendly throttling. The TPL abstracts thread management, work stealing, and load balancing.
Previous
What is the difference between eager, lazy, and explicit loading in EF Core?
Next
What is a CancellationToken in C#?
More C# / .NET Questions
View all →- Intermediate What is ASP.NET Core and how does it differ from ASP.NET Framework?
- Intermediate What is middleware in ASP.NET Core?
- Intermediate What is dependency injection in ASP.NET Core?
- Intermediate What is Entity Framework Core?
- Intermediate What is the difference between eager, lazy, and explicit loading in EF Core?