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
4
votes
1 answer

C# - QueuedTaskScheduler - threadCount vs maxConcurrencyLevel

I see the below from QueuedTaskScheduler.cs documentation, but its not very clear to me. threadCount - The number of threads to create and use for processing work items. maxConcurrencyLevel - The maximum degree of concurrency allowed for this…
user441918
  • 165
  • 10
4
votes
1 answer

Does ConfigureAwait affect non-threadpool threads only?

I am playing a little bit with ConfigureAwait because I would like to understand how it works. Therfore, I wrote the following small console application (actually running in LINQPad): void Main() { // Use dispatcher to execute the code on STA…
JanDotNet
  • 3,746
  • 21
  • 30
4
votes
3 answers

InvalidCastException when using Task return value

I have spend almost two days reading async/await tutorials and answers on Stackoverflow and trying to understand asynchronous and parallel execution in C#. Still I can not get it to work with my code. What I need Execute a Active Directory search…
4
votes
4 answers

Backgroundworker and TPL's Task have the same ManagedThreadID?

I have a Backgroundworker whose purpose is to run jobs sequentially in the background. Now one job is implemented in multithreading way. That mean, the Backgroundworker will create several threads. I use Task Parallel Library so I use the…
Steve
  • 4,935
  • 11
  • 56
  • 83
4
votes
1 answer

OperationCanceledException thrown in tpl dataflow block gets swallowed

For some reason when an OperationCanceledException gets thrown inside an IDataflowBlock, the block does not propagate this exception to its IDataflowBlock.Completion task. Running the code sample below returns an unexpected…
4
votes
3 answers

Performances of PLINQ vs TPL

I have some DB operations to perform and I tried using PLINQ: someCollection.AsParallel() .WithCancellation(token) .ForAll(element => ExecuteDbOperation(element)) And I notice it is quite slow compared to: var tasks =…
Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45
4
votes
1 answer

Is there an effective way to use System.Threading.Tasks with WCF service proxies?

After reading a recent MSDN Magazine article about the Task scheduler I was hoping (and actually quite excited) that using it would benefit my use of WCF generated proxies. I was hoping to get some of the following benefits : 1) Ability to abort a…
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
4
votes
1 answer

Waiting for all tasks to finish using Task Parallel Library in .NET 4.0

Is there a shorter way to wait for multiple threads to finish? Maybe using ContinueWhenAll... but I don't want to run the rest of my code async. List objList = // something List taskHandles = new List(); for(int i = 0; i <…
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
4
votes
2 answers

How can I cancel a hung Task?

We have a Windows service that calls a 3rd party method that can hang when improperly configured by the end user, which is difficult to test for beforehand. We're handling this risk by calling the method in a Task with a timeout: private int?…
Oblivious Sage
  • 3,326
  • 6
  • 37
  • 58
4
votes
2 answers

Tasks in C# - unclear outcome using Random

I'm learning about async programming in C#, and written this code to test Task Parallel Library (Console application): static void Main(string[] args) { Stopwatch sw = new Stopwatch(); var opr1 = new SlowOperation(); var opr2 = new…
Bartosz
  • 4,542
  • 11
  • 43
  • 69
4
votes
1 answer

How to Catch Exceptions for an array of Tasks

I would like to understand how what I should be using inside of my "try" block when trying to await an Array of Tasks. I want all tasks to be awaited, regardless if one of them threw an Exception, so that they can all complete. Should I use: var…
blgrnboy
  • 4,877
  • 10
  • 43
  • 94
4
votes
1 answer

Why does ContinueWith pass the Task as the parameter

I have a Task t1. I want to run another Task t2 after t1 completes. I choose to use the .ContinueWith method of t1. void ThenFrob(Task t1) { t1.ContinueWith(frobber => frobber.Frob()) } Except, I cannot do this, because the Action…
psaxton
  • 1,693
  • 19
  • 24
4
votes
2 answers

BatchBlock produces batch with elements sent after TriggerBatch()

I have a Dataflow pipeline consisting of several blocks. When elements are flowing through my processing pipeline, I want to group them by field A. To do this I have a BatchBlock with high BoundedCapacity. In it I store my elements until I decide…
wojciech_rak
  • 2,276
  • 2
  • 21
  • 30
4
votes
1 answer

Is it possible to run async callbacks on the "main" thread in a console app?

In a GUI application, there are a ton of ways to run asynchronous code and have the result run on your "main" thread: Use async/await Use a BackgroundWorker someControl.Invoke() .ContinueWith(...,…
4
votes
3 answers

BlockingCollection with Parallel.For hangs?

I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var…
1 2 3
99
100