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
283
votes
10 answers

Parallel foreach with asynchronous lambda

I would like to handle a collection in parallel, but I'm having trouble implementing it and I'm therefore hoping for some help. The trouble arises if I want to call a method marked async in C#, within the lambda of the parallel loop. For…
clausndk
  • 3,129
  • 2
  • 18
  • 14
281
votes
13 answers

Calling async method synchronously

I have an async method: public async Task GenerateCodeAsync() { string code = await GenerateCodeService.GenerateCodeAsync(); return code; } I need to call this method from a synchronous method. How can I do this without having to…
Catalin
  • 11,503
  • 19
  • 74
  • 147
280
votes
4 answers

Parallel.ForEach vs Task.Factory.StartNew

What is the difference between the below code snippets? Won't both be using threadpool threads? For instance if I want to call a function for each item in a collection, Parallel.ForEach(items, item => DoSomething(item)); vs foreach(var item…
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
268
votes
3 answers

await vs Task.Wait - Deadlock?

I don't quite understand the difference between Task.Wait and await. I have something similar to the following functions in a ASP.NET WebAPI service: public class TestController : ApiController { public static async Task Foo() { …
ronag
  • 49,529
  • 25
  • 126
  • 221
256
votes
9 answers

HttpClient - A task was cancelled?

It works fine when have one or two tasks however throws an error "A task was cancelled" when we have more than one task listed. List allTasks = new List(); allTasks.Add(....); allTasks.Add(....); Task.WaitAll(allTasks.ToArray(),…
One Developer
  • 99
  • 5
  • 43
  • 103
255
votes
11 answers

When should TaskCompletionSource be used?

AFAIK, all it knows is that at some point, its SetResult or SetException method is being called to complete the Task exposed through its Task property. In other words, it acts as the producer for a Task and its completion. I saw here the…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
249
votes
7 answers

What is the use for Task.FromResult in C#

In C# and TPL (Task Parallel Library), the Task class represents an ongoing work that produces a value of type T. I'd like to know what is the need for the Task.FromResult method ? That is: In a scenario where you already have the produced value at…
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
233
votes
4 answers

Parallel.ForEach vs Task.Run and Task.WhenAll

What are the differences between using Parallel.ForEach or Task.Run() to start a set of tasks asynchronously? Version 1: List strings = new List { "s1", "s2", "s3" }; Parallel.ForEach(strings, s => { DoSomething(s); }); Version…
Petter T
  • 3,387
  • 2
  • 19
  • 31
232
votes
8 answers

Create a completed Task

I want to create a completed Task (not Task). Is there something built into .NET to do this? A related question: Create a completed Task
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
229
votes
4 answers

Cancellation token in Task constructor: why?

Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter: CancellationTokenSource source = new CancellationTokenSource(); Task t = new Task (/* method */, source.Token); What baffles me about this is that there is no…
Colin
  • 2,693
  • 4
  • 20
  • 14
227
votes
11 answers

Nesting await in Parallel.ForEach

In a metro app, I need to execute a number of WCF calls. There are a significant number of calls to be made, so I need to do them in a parallel loop. The problem is that the parallel loop exits before the WCF calls are all complete. How would you…
Darthg8r
  • 12,377
  • 15
  • 63
  • 100
222
votes
12 answers

No ConcurrentList in .Net 4.0?

I was thrilled to see the new System.Collections.Concurrent namespace in .Net 4.0, quite nice! I've seen ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, ConcurrentBag and BlockingCollection. One thing that seems to be mysteriously missing…
Alan
  • 2,574
  • 2
  • 18
  • 16
221
votes
6 answers

What's the difference between Task.Start/Wait and Async/Await?

I may be missing something but what is the difference between doing: public void MyMethod() { Task t = Task.Factory.StartNew(DoSomethingThatTakesTime); t.Wait(); UpdateLabelToSayItsComplete(); } public async void MyMethod() { var result =…
Jon
  • 38,814
  • 81
  • 233
  • 382
210
votes
7 answers

When to dispose CancellationTokenSource?

The class CancellationTokenSource is disposable. A quick look in Reflector proves usage of KernelEvent, a (very likely) unmanaged resource. Since CancellationTokenSource has no finalizer, if we do not dispose it, the GC won't do it. On the other…
190
votes
5 answers

Async/await vs BackgroundWorker

In the past few days I have tested the new features of .net 4.5 and c# 5. I like its new async/await features. Earlier I had used BackgroundWorker to handle longer processes in the background with responsive UI. My question is: after having these…
Tom
  • 3,899
  • 22
  • 78
  • 137