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 TaskCompletionSource.Task
. Here is a simple example to illustrate the question:
async ValueTask DispatcherTimerDelayAsync(TimeSpan delay)
{
var tcs = new TaskCompletionSource<bool>();
var timer = new DispatcherTimer();
timer.Interval = delay;
timer.Tick += (s, e) => tcs.SetResult(true);
timer.Start();
try
{
await tcs.Task;
}
finally
{
timer.Stop();
}
}
Should I rather be returning Task
(versus ValueTask
) from DispatcherTimerDelayAsync
, which itself is always expected to be asynchronous?