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
2
votes
2 answers

ValueTask method with unavoidable async await calls?

I currently have the following async method: private SomeObject _someObject = null; public async Task GetObjectAsync() { await sslim.WaitAsync(); if (_someObject == null) { _someObject = await…
2
votes
1 answer

Awaiting a single .NET event with a ValueTask

I have a simple ITimer interface that has just a classic Elapsed .NET event that is raised after the a certain timespan. interface ITimer { void Start(TimeSpan interval); void Stop(); public event EventHandler Elapsed; } Now I would…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
1
vote
1 answer

Testing ValueTask method with FluentAssertions, should not throw

I'm a bit confused here. I'm using FluentAssertions and I have this test method: [Test] public void DoNothingWhenEmpty() { FileCleanUpProcessor fileProcessor = new(); Func act = () =>…
AlexB
  • 4,167
  • 4
  • 45
  • 117
1
vote
0 answers

CA2012 warning when using ValueTask with discard

I am making a simple TCP server and stumbled on this article by Marc Gravell mentioning the following: Use ValueTask[], unless you absolutely can't because the existing API is Task[], and even then: at least consider an API break So I changed my…
Moaaz Assali
  • 147
  • 1
  • 8
1
vote
1 answer

How to return inside a ValueTask returning lambda of Parallel.ForEachAsync?

In general we can await an async method invocation in any one of the Branch of execution. And other branches we can simply return. This will not give any warning. But when we do the same in Parallel.ForEachAsync, We get the warning CS4014 "Because…
Siva Sankaran
  • 1,521
  • 4
  • 21
  • 40
1
vote
2 answers

What's the right way to implement ValueTaskSource.SetCompleted

So we have this class implementing IValueTaskSource This code cannot be written as async-await because there's nothing to await on. We send a message to another running thread and get back a ValueTask that can be awaited by the caller to get the…
Joshua
  • 40,822
  • 8
  • 72
  • 132
1
vote
1 answer

Parallel.ForEachAsync Task vs ValueTask

I'm trying out Parallel.ForEachAsync and the compiler is kind enough to inform me that the body is a func that returns a ValueTask, not a Task. Stopwatch sw = Stopwatch.StartNew(); var numbers = Enumerable.Range(start: 0, count: 10); // Error:…
tymtam
  • 31,798
  • 8
  • 86
  • 126
1
vote
1 answer

ValueTask instances should not have their result directly accessed unless the instance has already completed

There is a library returning a ValueTask and I have a synchronous method which is consuming the ValueTask. The issue is that there is the following warning: CA2012: ValueTask instances should not have their result directly accessed unless the…
nop
  • 4,711
  • 6
  • 32
  • 93
1
vote
1 answer

Do I need to query the .Result property of a ValueTask that I choose to not await?

Let's say I have a method that returns an object of type ValueTask, for example the WaitToReadAsync method on ChannelReader. There are three ways I can consume this ValueTask. I can either: Directly await it. Turn it into a Task, then await it…
Tea
  • 160
  • 7
1
vote
2 answers

Should we use ValueTask within System.Threading.Channels.WaitToReadAsync loops?

Since we expect to be reading frequently, and for us to often be reading when data is already available to be consumed, should SendLoopAsync return ValueTask rather than Task, so that we can make it allocation-free? // Caller _ =…
nop
  • 4,711
  • 6
  • 32
  • 93
1
vote
1 answer

Run ValueTasks on a custom thread pool

I am looking to execute a bunch of ValueTask-returning functions on a custom thread pool - i.e. on a bunch of threads I'm spawning and handling myself, rather than the default ThreadPool. Meaning, all synchronous bits of these functions, including…
Bogey
  • 4,926
  • 4
  • 32
  • 57
1
vote
1 answer

Will ValueTask still be beneficial if it depends on a method that returns Task?

Using the following methods as an example: public async ValueTask MyMethod() { return await GetMyIntegerWithTask(); } public async Task GetMyIntegerWithTask() { if (/*in hot path*/) { return await…
asdf
  • 35
  • 3
1
vote
1 answer

ValueTask for synchronous method

I am implementing a simple lookup service using concurrent dictionary to store data. Since most of the methods using this service will be async I am considering exposing the lookup functions with ValueTask. Pseudocode: public…
NullReference
  • 862
  • 1
  • 11
  • 27
1
vote
2 answers

Is it necessary to check `ValueTask.IsCompleted` for performance?

case 1: bool result = await DoAsync(); case 2: ValueTask task = DoAsync(); bool result = task.IsCompleted ? task.Result : await task; With case 1 and case 2 above, can anyone say case 2 is better for performance(cpu, memory, etc.)? Or,…
ALittleDiff
  • 1,191
  • 1
  • 7
  • 24
0
votes
1 answer

How can I synchronously wait for ValueTask without performance loss?

First of all, our platform for this question is C# 9.0 and .NET core 7.0. Lets suppose that I have to wait for a ValueTask to complete in a synchronous method. When dealing with tasks in this context, I generally use the following scheme: var task =…