Questions tagged [valuetask]

A value type that wraps a Task and a TResult, only one of which is used.

Documentation of ValueTask<TResult> struct.
More reading: Understanding the Whys, Whats, and Whens of ValueTask.

35 questions
41
votes
5 answers

Task.WhenAll for ValueTask

Is there an equivalent of Task.WhenAll accepting ValueTask? I can work around it using Task.WhenAll(tasks.Select(t => t.AsTask())) This will be fine if they're all wrapping a Task but it will force the useless allocation of a Task object for real…
Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45
29
votes
2 answers

What is the ValueTask equivalent of Task.CompletedTask?

I am implementing IAsyncDisposable which requires me to return a ValueTask, but sometimes my dispose method has nothing to do. How should I return in this case? At the moment I'm returning new ValueTask(Task.CompletedTask) which seems to work but…
Andy
  • 10,412
  • 13
  • 70
  • 95
12
votes
1 answer

Should I await ValueTask?

Which would be a valid implementation of ValueTask please? Cache service returns data either from cache or DB. public async ValueTask> GetEmployeesFacts() { try { var facts =…
sziszu
  • 490
  • 1
  • 6
  • 16
7
votes
2 answers

Should I return ValueTask if I still use TaskCompletionSource to implement asynchrony?

Is there still a benefit in returning ValueTask if I use TaskCompletionSource to implement actual asynchrony? As I understand it, the purpose of ValueTask is to reduce allocations, but allocations are still there when awaiting…
avo
  • 10,101
  • 13
  • 53
  • 81
7
votes
2 answers

The ValueTask and the async state machine

According to the documentation a ValueTask... Provides a value type that wraps a Task and a TResult, only one of which is used. My question is about the state machine that the C# compiler generates when the async keyword is…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
5
votes
1 answer

Why is Task faster than ValueTask?

I'm doing a benchmark on Task and ValueTask. Source code below: #LINQPad optimize+ // Enable compiler optimizations void Main() { Util.AutoScrollResults = true; …
Will Huang
  • 2,955
  • 2
  • 37
  • 90
5
votes
2 answers

Convert a ValueTask to a non generic ValueTask

The question asked here is the same as the one here and is aimed to create a definitive solution to it. The most accurate answer is by Stephen Toub himself in this issue that is exactly about this question. The "recommendended code" is the following…
Spi
  • 686
  • 3
  • 18
5
votes
3 answers

Is there any sense to return ValueTask from method that internally awaits some Tasks

I have an async method that looks approximately like this: async Task ParseStream() { var a = await reader.ReadInt32(); var b = await reader.ReadInt32(); return a + b; } This method is going to work synchronously most of the time…
wiuwiu
  • 63
  • 1
  • 5
4
votes
2 answers

What is the point of ValueTask.Preserve()?

ValueTask and ValueTask have a Preserve() method which is summarized as "Gets a ValueTask that may be used at any point in the future." What does that mean and when should we use it? Does it imply that a 'normal' ValueTask can't be used "at…
Petrusion
  • 940
  • 4
  • 11
4
votes
0 answers

EF Core DbSet AddAsync method: Should the async method be used in 100% of the cases?

The documentation on the AddAsync method states that This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database…
0xDECAFBAD
  • 627
  • 1
  • 8
  • 21
4
votes
1 answer

How to choose the return type of an async delegate

I am passing an async delegate to the LINQ Select method, and I would prefer to get a list of ValueTasks instead of a list of Tasks. How can I do it? Example: var result = (new[] { 0 }).Select(async x => await…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
3
votes
3 answers

Why can an async method with return type ValueTask await a regular Task without generating a compiler error?

The following code does not compile public ValueTask Foo() { return Task.Delay(1000); } but yields an Error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask as expected. However,…
Joerg
  • 790
  • 2
  • 10
  • 23
3
votes
3 answers

Is there a ContinueWith for ValueTask?

If an API returns a ValueTask or ValueTask, is there a way to perform a ContinueWith on it, like I'm able to do with Task? Is there a Microsoft-provided NuGet library for doing so with .NET Standard 2.0?
D.R.
  • 20,268
  • 21
  • 102
  • 205
2
votes
2 answers

How to Pause/Resume an asynchronous worker, without incurring memory allocation overhead?

I have an asynchronous method that contains an endless while loop. This loop is paused and resumed frequently by the main thread of the program. For this purpose I am using currently the PauseTokenSource class from the Nito.AsyncEx package: private…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
2
votes
2 answers

How to return a canceled ValueTask that propagates an OperationCanceledException, without async/await?

I am writting an API that has a ValueTask return type, and accepts a CancellationToken. In case the CancellationToken is already canceled upon invoking the method, I would like to return a canceled ValueTask (IsCanceled == true), that…
1
2 3