Questions tagged [task-parallel-library]

The Task Parallel Library is part of the .NET Framework since .NET 4. It is a set of APIs that simplifies the process of adding parallelism and concurrency to applications.

The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and and the System.Threading.Tasks namespaces in the .NET Framework 4 and up. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. In addition, the TPL handles the partitioning of the work, the scheduling of threads on the ThreadPool, cancellation support, state management, and other low-level details.

Related Tags

Free resources

6189 questions
187
votes
4 answers

How to cancel a Task in await?

I'm playing with these Windows 8 WinRT tasks, and I'm trying to cancel a task using the method below, and it works to some point. The CancelNotification method DOES get called, which makes you think the task was cancelled, but in the background the…
Carlo
  • 25,602
  • 32
  • 128
  • 176
185
votes
2 answers

Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

I just saw 3 routines regarding TPL usage which do the same job; here is the code: public static void Main() { Thread.CurrentThread.Name = "Main"; // Create a task and supply a user delegate by using a lambda expression. Task taskA =…
Mou
  • 15,673
  • 43
  • 156
  • 275
183
votes
6 answers

Why should I prefer single 'await Task.WhenAll' over multiple awaits?

In case I do not care about the order of task completion and just need them all to complete, should I still use await Task.WhenAll instead of multiple await? e.g, is DoWork2 below a preferred method to DoWork1 (and why?): using System; using…
avo
  • 10,101
  • 13
  • 53
  • 81
172
votes
5 answers

Why CancellationToken is separate from CancellationTokenSource?

I'm looking for a rationale of why .NET CancellationToken struct was introduced in addition to CancellationTokenSource class. I understand how the API is to be used, but want to also understand why it is designed that way. I.e., why do we have: var…
163
votes
5 answers

Default parameter for CancellationToken

I have some async code that I would like to add a CancellationToken to. However, there are many implementations where this is not needed so I would like to have a default parameter - perhaps CancellationToken.None. However, Task DoStuff(....,…
tofutim
  • 22,664
  • 20
  • 87
  • 148
155
votes
2 answers

Difference between await and ContinueWith

Can someone explain if await and ContinueWith are synonymous or not in the following example. I'm trying to use TPL for the first time and have been reading all the documentation, but don't understand the difference. Await: String webText = await…
Harrison
  • 3,843
  • 7
  • 22
  • 49
149
votes
5 answers

How can I wait till the Parallel.ForEach completes

I'm using TPL in my current project and using Parallel.Foreach to spin many threads. The Task class contains Wait() to wait till the task gets completed. Like that, how I can wait for the Parallel.ForEach to complete and then go into executing next…
VJAI
  • 32,167
  • 23
  • 102
  • 164
149
votes
3 answers

Need to understand the usage of SemaphoreSlim

Here is the code I have but I don't understand what SemaphoreSlim is doing. async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); List trackedTasks = new List(); while (DoMore()) { await…
Mou
  • 15,673
  • 43
  • 156
  • 275
149
votes
12 answers

How to limit the amount of concurrent async I/O operations?

// let's say there is a list of 1000+ URLs string[] urls = { "http://google.com", "http://yahoo.com", ... }; // now let's send HTTP requests to each of these URLs in parallel urls.AsParallel().ForAll(async (url) => { var client = new…
Grief Coder
  • 6,508
  • 9
  • 38
  • 51
146
votes
4 answers

What's the difference between returning void and returning a Task?

In looking at various C# Async CTP samples I see some async functions that return void, and others that return the non-generic Task. I can see why returning a Task is useful to return data to the caller when the async operation completes,…
James Cadd
  • 12,136
  • 30
  • 85
  • 134
146
votes
6 answers

Run two async tasks in parallel and collect results in .NET 4.5

I've been trying for a while to get something I thought would be simple working with .NET 4.5 I want to fire off two long running tasks at same time and collect the results in in the best C# 4.5 (RTM) way The following works but I don't like it…
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
141
votes
6 answers

Create a completed Task

I'm implementing a method Task StartSomeTask() and happen to know the result already before the method is called. How do I create a Task that has already completed? This is what I'm currently doing: private readonly Result theResult =…
dtb
  • 213,145
  • 36
  • 401
  • 431
133
votes
3 answers

Is it considered acceptable to not call Dispose() on a TPL Task object?

I want to trigger a task to run on a background thread. I don't want to wait on the tasks completion. In .net 3.5 I would have done this: ThreadPool.QueueUserWorkItem(d => { DoSomething(); }); In .net 4 the TPL is the suggested way. The common…
Simon P Stevens
  • 27,303
  • 5
  • 81
  • 107
131
votes
4 answers

Simplest way to do a fire and forget method in c# 4.0

I really like this question: Simplest way to do a fire and forget method in C#? I just want to know that now that we have Parallel extensions in C# 4.0 is there a better cleaner way to do Fire & Forget with Parallel linq?
Jonathon Kresner
  • 2,793
  • 5
  • 29
  • 40
129
votes
10 answers

ASP.NET Web API OperationCanceledException when browser cancels the request

When a user loads a page, it makes one or more ajax requests, which hit ASP.NET Web API 2 controllers. If the user navigates to another page, before these ajax requests complete, the requests are canceled by the browser. Our ELMAH HttpModule then…