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
128
votes
5 answers

An async/await example that causes a deadlock

I came across some best practices for asynchronous programming using c#'s async/await keywords (I'm new to c# 5.0). One of the advices given was the following: Stability: Know your synchronization contexts ... Some synchronization contexts are…
dror
  • 3,759
  • 6
  • 31
  • 45
120
votes
3 answers

What is the advantage of using async with MVC5?

What is the difference between: public ActionResult Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { IdentityResult result = IdentityManager.Authentication.CheckPasswordAndSignIn(AuthenticationManager,…
116
votes
5 answers

Why does this async action hang when I try and access the Result property of my Task?

I have a multi-tier .Net 4.5 application calling a method using C#'s new async and await keywords that just hangs and I can't see why. At the bottom I have an async method that extents our database utility OurDBConn (basically a wrapper for the…
Keith
  • 150,284
  • 78
  • 298
  • 434
112
votes
8 answers

Task.Run with Parameter(s)?

I have implemented a simple Task.Factory.StartNew() and I wonder how can I do it with Task.Run() instead? Here is the basic code: Task.Factory.StartNew(new Action( (x) => { // Do something with 'x' }), rawData); I looked into…
Thus Spoke Nomad
  • 2,372
  • 4
  • 17
  • 34
110
votes
2 answers

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was

What does this mean and how to resolve it? I am using TPL tasks. The whole error A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the…
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
107
votes
3 answers

The current SynchronizationContext may not be used as a TaskScheduler

I am using Tasks to run long running server calls in my ViewModel and the results are marshalled back on Dispatcher using TaskScheduler.FromSyncronizationContext(). For example: var context =…
anivas
  • 6,437
  • 6
  • 37
  • 45
106
votes
4 answers

Is there anything like asynchronous BlockingCollection?

I would like to await on the result of BlockingCollection.Take() asynchronously, so I do not block the thread. Looking for anything like this: var item = await blockingCollection.TakeAsync(); I know I could do this: var item = await Task.Run(()…
avo
  • 10,101
  • 13
  • 53
  • 81
105
votes
5 answers

Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'

I am new to asynchronous programming, so after going through some async sample codes, I thought of writing a simple async code I created a simple Winform application and inside the Form I wrote the following code. But its just not working private…
techBeginner
  • 3,792
  • 11
  • 43
  • 59
102
votes
7 answers

Is there a Task based replacement for System.Threading.Timer?

I'm new to .Net 4.0's Tasks and I wasn't able to find what I thought would be a Task based replacement or implementation of a Timer, e.g. a periodic Task. Is there such a thing? Update I came up with what I think is a solution to my needs which is…
Jim
  • 4,910
  • 4
  • 32
  • 50
101
votes
3 answers

Proper way to implement a never ending task. (Timers vs Task)

So, my app needs to perform an action almost continuously (with a pause of 10 seconds or so between each run) for as long as the app is running or a cancellation is requested. The work it needs to do has the possibility of taking up to 30…
Josh
  • 2,083
  • 5
  • 23
  • 28
99
votes
4 answers

Calling async methods from non-async code

I'm in the process of updating a library that has an API surface that was built in .NET 3.5. As a result, all methods are synchronous. I can't change the API (i.e., convert return values to Task) because that would require that all callers change.…
Erick T
  • 7,009
  • 9
  • 50
  • 85
98
votes
6 answers

Does the use of async/await create a new thread?

I am new to TPL and I am wondering: How does the asynchronous programming support that is new to C# 5.0 (via the new async and await keywords) relate to the creation of threads? Specifically, does the use of async/await create a new thread each time…
dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
96
votes
5 answers

How to cancel a CancellationToken

I start a task, that starts other tasks and so forth. Given that tree, if any task fails the result of the whole operation is useless. I'm considering using cancellation tokens. To my surprise, the token does not have a "CancelThisToken()"…
Leonardo
  • 10,737
  • 10
  • 62
  • 155
93
votes
3 answers

Deserialize JSON to Array or List with HTTPClient .ReadAsAsync using .NET 4.0 Task pattern

I'm trying to deserialize the JSON returned from http://api.usa.gov/jobs/search.json?query=nursing+jobs using the .NET 4.0 Task pattern. It returns this JSON ('Load JSON data' @ http://jsonviewer.stack.hu/). [ { "id": "usajobs:353400300", …
Joe
  • 4,143
  • 8
  • 37
  • 65
93
votes
2 answers

What is the best way to catch exception in Task?

With System.Threading.Tasks.Task, I have to manage the exceptions that could be thrown. I'm looking for the best way to do that. So far, I've created a base class that manages all the uncaught exceptions inside the call of…
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57