6,298 questions
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 ...
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(&...
0 votes
0 answers
108 views
Hope to cope with exceptions inside tasks
Here's my example code. It's of no use, but it's short. I have an array to switch on or off the creation of Exceptions at different parts of my code and two functions. The first (Level 0) is an ...
0 votes
2 answers
96 views
Crucial differences of await keyword and ContinueWith()
For the following code snippets, I'm wondering what the actual differences are, when they are executed. The first snippet using await: public async Task LoadAsync() { task = loader.LoadAsync(...); ...
1 vote
1 answer
135 views
How does TaskCompletionSource work when an exception occurs?
I'm trying to understand how exception handling works with TaskCompletionSource and await in a console application (which has no SynchronizationContext). I've written simple code that waits for a ...
0 votes
1 answer
208 views
Why does Task.WhenAll fail to Complete when all its input tasks are completed if locks are involved
See below for complete minimal repro (net9 console project). When it deadlocks, and I attach a debugger, I can see that the only input task to the whenAllTask is completed, yet the whenAllTask itself ...
2 votes
1 answer
137 views
Task.WhenEach is appending to the task list safe?
Take the example below with the new Task.WhenEach method List<Task<List<int>>> tasks = [ MyTask() ]; await foreach (var task in Task.WhenEach(tasks)) { var result = await ...
0 votes
1 answer
86 views
AsParallel.ForAll with async code doesn't wait for completion [duplicate]
I've got some code which uses AsParallel().ForAll(...) which I was led to believe would block while all of the executions completed. This works perfectly with non async code. For example the below ...
2 votes
1 answer
190 views
How to wrap a Task in a Task<TResult> without using an async state machine?
I want to wrap a Task in a Task<TResult> without using an async state machine, while preserving the original task's properties. Based on What's the best way to wrap a Task as a Task<TResult&...
0 votes
0 answers
57 views
How can I create a delay task which contains a result value? [duplicate]
I have an override method which looks like this: public override Task Start(CancellationToken cancellationToken) { return Task.Delay(1000,cancellationToken); } However the abstract class ...
1 vote
3 answers
254 views
Why are Minimal API endpoints waiting for Tasks?
Building a basic ASP.NET Core Minimal API, I was surprised by the fact that if an endpoint returns a Task, the request does not return until the Task is done: var builder = WebApplication....
1 vote
2 answers
113 views
Why do `ParallelQuery.ToArray()` and `ToList()` return ordered sequence?
The doc listed the operators in the table that ToArray should be unordered when the parallelquery source is unordered. However, the result turned out to be always ordered when force evaluated to array ...
0 votes
2 answers
120 views
Any side effects when writing LINQ-like extensions for Task<IEnumerable<T>>?
I'm a little bit annoyed of parentheses in situations like this: var items = (await SomeService.GetDataAsEnumerableAsync()).ToList(); So I thought of creating an extension method like: public static ...
1 vote
2 answers
71 views
Task.WaitAll does not throw as expected
I was expecting Task.WaitAll to throw when semaphore reaches its maxcount, but the result is, all tasks were hanged. SemaphoreSlim semaphore = new(initialCount: 3, maxCount: 10); var tasks = ...