6,298 questions
3 votes
1 answer
2k views
How to change batch size of batchblock dynamically during runtime?
I have a batch block in tpl dataflow and have several target blocks linked to the batch block. However, the number of target blocks changes dynamically and thus the size of the batches. Problem is ...
28 votes
14 answers
26k views
How can I assign a name to a task in TPL
I'm going to use lots of tasks running on my application. Each bunch of tasks is running for some reason. I would like to name these tasks so when I watch the Parallel Tasks window, I could recognize ...
0 votes
1 answer
123 views
Why are items not written to console immediately after being processed?
I have the following C# code : var rand = new Random(1); var range = Enumerable.Range(1, 8); var partition = Partitioner.Create(range, EnumerablePartitionerOptions.NoBuffering); foreach (var x in ...
17 votes
5 answers
6k views
How to transform task.Wait(CancellationToken) to an await statement?
So, task.Wait() can be transformed to await task. The semantics are different, of course, but this is roughly how I would go about transforming a blocking code with Waits to an asynchronous code with ...
3 votes
6 answers
2k views
Task.WhenAll but process results one by one
Let's say I have a list of Tasks, and I want to run them in parallel. But I don't need all of them to finish to continue, I can move on with just one. The following code waits for all the tasks to ...
8 votes
3 answers
13k views
How to check that all tasks have been properly completed?
I have the following lines in my code: var taskA = Task.Factory.StartNew(WorkA); var taskB = Task.Factory.StartNew(WorkB); var allTasks = new[] { taskA, taskB }; Task.Factory....
5 votes
2 answers
25k views
Specify default MaxDegreeOfParallelism in Parallel.ForEach?
We would like to optionally control the number of "threads" on our parallel loops to avoid overwhelming a web service (for example). Is it possible to specify a custom MaxDegreeOfParallelism on a ...
8 votes
1 answer
2k views
Sort Tasks into order of completion
I saw Jon Skeet give a talk a year or so ago where he showed a snippet of C# 5 that would take a list of Tasks and return them in the order they completed. It made use of async/await and WhenAny and ...
6 votes
3 answers
11k views
Limited concurrency level task scheduler (with task priority) handling wrapped tasks
I'm having a hard time finding a task scheduler on which I can schedule prioritised tasks but can also handle "wrapped" tasks. It is something like what Task.Run tries to solve, but you ...
1 vote
2 answers
175 views
Task in C# is cancelling by itself [duplicate]
I have some code in a dll project with the target as .Net 8. In my code there is an async method called EmailNonError which is being called with the following code. When this code executes, the email ...
0 votes
1 answer
59 views
WhenAll nested in WhenAny (or WhenAll with timeout) [duplicate]
The first exception does not block the execution: public static async Task Main() { await Task.WhenAny( Task.WhenAll( Task.Run(() => throw new Exception(&...
18 votes
8 answers
18k views
How can I unit test code that contain Task.Delay?
How can I unit test component that has an await Task.Delay without really having to wait for it. For example, public void Retry() { // doSomething(); if(fail) await Task.Delay(5000); ...
16 votes
6 answers
4k views
Is there a way to Wait for a TPL Task without in throwing an exception?
Some of us prefer to code in an exception-light style. However, if you wait for a Task Parallel Library task, and the task threw an exception, it will throw an exception on the calling thread as well. ...
13 votes
3 answers
9k views
PagedList and Async
I'm using PagedList in my Views, but my scaffolded Controller is generated with this kind of default Index Action: public async Task<ActionResult> Index() { return View(await db.Claimants....
51 votes
5 answers
42k views
Set ApartmentState on a Task
I am trying to set the apartment state on a task but see no option in doing this. Is there a way to do this using a Task? for (int i = 0; i < zom.Count; i++) { Task t = Task.Factory.StartNew(...