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
671
votes
4 answers

Best practice to call ConfigureAwait for all server-side code

When you have server-side code (i.e. some ApiController) and your functions are asynchronous - so they return Task - is it considered best practice that any time you await functions that you call ConfigureAwait(false)? I had read that it…
Aen
  • 7,433
  • 3
  • 20
  • 21
614
votes
10 answers

When to use Task.Delay, when to use Thread.Sleep?

Are there good rule(s) for when to use Task.Delay versus Thread.Sleep? Specifically, is there a minimum value to provide for one to be effective/efficient over the other? Lastly, since Task.Delay causes context-switching on a async/await state…
Tom K.
  • 6,549
  • 3
  • 14
  • 14
555
votes
9 answers

If my interface must return Task what is the best way to have a no-operation implementation?

In the code below, due to the interface, the class LazyBar must return a task from its method (and for argument's sake can't be changed). If LazyBars implementation is unusual in that it happens to run quickly and synchronously - what is the best…
Jon Rea
  • 9,337
  • 4
  • 32
  • 35
525
votes
20 answers

Asynchronously wait for Task to complete with timeout

I want to wait for a Task to complete with some special rules: If it hasn't completed after X milliseconds, I want to display a message to the user. And if it hasn't completed after Y milliseconds, I want to automatically request…
dtb
  • 213,145
  • 36
  • 401
  • 431
525
votes
8 answers

Using async/await for multiple tasks

I'm using an API client that is completely asynchrounous, that is, each operation either returns Task or Task, e.g: static async Task DoSomething(int siteId, int postId, IBlogClient client) { await client.DeletePost(siteId, postId); // call…
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
500
votes
4 answers

WaitAll vs WhenAll

What is the difference between Task.WaitAll() and Task.WhenAll() from the Async CTP? Can you provide some sample code to illustrate the different use cases?
Yaron Levi
  • 12,535
  • 16
  • 69
  • 118
486
votes
13 answers

How to safely call an async method in C# without await

I have an async method which returns no data: public async Task MyAsyncMethod() { // do some stuff async, don't return any data } I'm calling this from another method which returns some data: public string GetStringData() { MyAsyncMethod();…
George Powell
  • 9,141
  • 8
  • 35
  • 43
447
votes
5 answers

How can I tell Moq to return a Task?

I've got an interface which declares Task DoSomethingAsync(); I'm using MoqFramework for my tests: [TestMethod()] public async Task MyAsyncTest() { Mock mock = new Mock(); mock.Setup(arg =>…
Waldemar
  • 5,363
  • 3
  • 17
  • 28
435
votes
8 answers

What is the difference between task and thread?

In C# 4.0, we have Task in the System.Threading.Tasks namespace. What is the true difference between Thread and Task. I did some sample program(help taken from MSDN) for my own sake of learning with Parallel.Invoke Parallel.For Parallel.ForEach…
user372724
387
votes
12 answers

Awaiting multiple Tasks with different results

I have 3 tasks: private async Task FeedCat() {} private async Task SellHouse() {} private async Task BuyCar() {} They all need to run before my code can continue and I need the results from each as well. None of the results have…
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
384
votes
11 answers

Running multiple async tasks and waiting for them all to complete

I need to run multiple async tasks in a console application, and wait for them all to complete before further processing. There's many articles out there, but I seem to get more confused the more I read. I've read and understand the basic principles…
Daniel Minnaar
  • 5,865
  • 5
  • 31
  • 52
376
votes
5 answers

Synchronously waiting for an async operation, and why does Wait() freeze the program here

Preface: I'm looking for an explanation, not just a solution. I already know the solution. Despite having spent several days studying MSDN articles about the Task-based Asynchronous Pattern (TAP), async and await, I'm still a bit confused about some…
354
votes
6 answers

Catch an exception thrown by an async void method

Using the async CTP from Microsoft for .NET, is it possible to catch an exception thrown by an async method in the calling method? public async void Foo() { var x = await DoSomethingAsync(); /* Handle the result, but sometimes an exception…
TimothyP
  • 21,178
  • 26
  • 94
  • 142
316
votes
3 answers

Is there an equivalent to 'continue' in a Parallel.ForEach?

I am porting some code to Parallel.ForEach and got an error with a continue I have in the code. Is there something equivalent I can use in a Parallel.ForEach functionally equivalent to continue in a foreach loop? Parallel.ForEach(items,…
297
votes
6 answers

What is the difference between Task.Run() and Task.Factory.StartNew()

I have Method : private static void Method() { Console.WriteLine("Method() started"); for (var i = 0; i < 20; i++) { Console.WriteLine("Method() Counter = " + i); Thread.Sleep(500); } …
Sergiy Lichenko
  • 3,102
  • 2
  • 11
  • 10
1
2 3
99 100