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
92
votes
2 answers

Best way to convert callback-based async method to awaitable task

What would be the best way to convert/wrap a "classic" asynchronous method that uses a callback to something that returns a (awaitable) Task? For example, given the following method: public void GetStringFromUrl(string url, Action
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
88
votes
7 answers

Is prevTask.Wait() recommended to be used with ContinueWith (from the Tasks library)?

So I was told recently that how I was using my .ContinueWith for Tasks was not the proper way to use them. I have yet to find evidence of this on the internet so I will ask you guys and see what the answer is. Here is an example of how I use…
Travyguy9
  • 4,774
  • 8
  • 43
  • 63
87
votes
5 answers

Simplest way to run three methods in parallel in C#

I have three methods that I call to do some number crunching that are as follows results.LeftFront.CalcAi(); results.RightFront.CalcAi(); results.RearSuspension.CalcAi(geom, vehDef.Geometry.LTa.TaStiffness,…
PlTaylor
  • 7,345
  • 11
  • 52
  • 94
86
votes
6 answers

How can I prevent synchronous continuations on a Task?

I have some library (socket networking) code that provides a Task-based API for pending responses to requests, based on TaskCompletionSource. However, there's an annoyance in the TPL in that it seems to be impossible to prevent synchronous…
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
84
votes
2 answers

ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew

What is difference between the below ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew If the above code is called 500 times for some long running task, does it mean all the thread pool threads will be taken up? Or will TPL (2nd option) be…
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
84
votes
2 answers

Task parallel library replacement for BackgroundWorker?

Does the task parallel library have anything that would be considered a replacement or improvement over the BackgroundWorker class? I have a WinForms application with a wizard-style UI, and it does some long-running tasks. I want to be able to have…
Keith G
  • 4,580
  • 5
  • 38
  • 48
83
votes
2 answers

How to create a task (TPL) running a STA thread?

Using Thread is pretty straightforward Thread thread = new Thread(MethodWhichRequiresSTA); thread.SetApartmentState(ApartmentState.STA); How to accomplish the same using Tasks in a WPF application? Here is some code: Task.Factory.StartNew …
Michel Triana
  • 2,486
  • 1
  • 22
  • 31
83
votes
6 answers

Should i use ThreadPools or Task Parallel Library for IO-bound operations

In one of my projects that's kinda an aggregator, I parse feeds, podcasts and so from the web. If I use sequential approach, given that a large number of resources, it takes quite a time to process all of them (because of network issues and similar…
83
votes
4 answers

When to use TaskCreationOptions.LongRunning?

I've wondered this for quite a while, but never really found the answer. I understand that it's a hint for the task scheduler where the task will run on, and that the task scheduler can (or nowadays will?) decide to instantiate a non-thread-pool…
bas
  • 13,550
  • 20
  • 69
  • 146
80
votes
3 answers

Getting return value from Task.Run

I have the following code: public static async Task Start(IProgress progress) { const int total = 10; for (var i = 0; i <= total; i++) { await Task.Run(() =>…
Null Reference
  • 11,260
  • 40
  • 107
  • 184
79
votes
5 answers

multiple awaits vs Task.WaitAll - equivalent?

In terms of performance, will these 2 methods run GetAllWidgets() and GetAllFoos() in parallel? Is there any reason to use one over the other? There seems to be a lot happening behind the scenes with the compiler so I don't find it…
vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
78
votes
4 answers

Is Task.Factory.StartNew() guaranteed to use another thread than the calling thread?

I am starting a new task from a function but I would not want it to run on the same thread. I don't care which thread it runs on as long as it is a different one (so the information given in this question does not help). Am I guaranteed that the…
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
77
votes
1 answer

Why does TaskCanceledException occur?

I have the following test code: void Button_Click(object sender, RoutedEventArgs e) { var source = new CancellationTokenSource(); var tsk1 = new Task(() => Thread1(source.Token), source.Token); var tsk2 = new Task(() =>…
net_prog
  • 9,921
  • 16
  • 55
  • 70
76
votes
4 answers

What is a replacement method for Task.Run in .NET 4.0 using C#?

I got this program that gives me syntax error "System.Threading.Tasks.task does not contain a definition for Run." I am using VB 2010 .NET 4.0 Any ideas? any replacements for Run in .net 4.0? using System; using System.Collections.Generic; using…
Ace Caserya
  • 2,825
  • 3
  • 25
  • 35
76
votes
3 answers

Difference between the TPL & async/await (Thread handling)

Trying to understanding the difference between the TPL & async/await when it comes to thread creation. I believe the TPL (TaskFactory.StartNew) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread…
coding4fun
  • 8,038
  • 13
  • 58
  • 85