Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy result from task 'A' to task 'B'?

Copy value from task 'A' to task 'B'.

This is entity example:

public class MachineConfiguration
{
      public Task<Dictionary<string, string>> LastReportTask { get; set; }
      public Task<Dictionary<string, string>> TempLastReportTask { get; set; }
}

My idea is to use value task to move result from one task to another. I am not sure if this is best solution.

var tempLastReportValueTask = new ValueTask<Dictionary<string, string>>(machineConfiguration.TempLastReportTask);
machineConfiguration.LastReportTask = Task.FromResult(tempLastReportValueTask.Result);
machineConfiguration.TempLastReportTask = null;
like image 692
Raskolnikov Avatar asked Aug 07 '26 08:08

Raskolnikov


1 Answers

Why not just assign it?

machineConfiguration.LastReportTask = machineConfiguration.TempLastReportTask;

By doing tempLastReportValueTask.Result, you are synchronously waiting for execution of the task and eliminating most of the benefits. If you need to touch the value (if you are doing some kind of processing there), you need to await it -- then there is little reason to store it as a task again as it's already evaluated and accessible.

Nevertheless, if you really need to do this to wrap already evaluated value to an interface, ValueTask is preferable from performance reasons.

like image 59
Lukáš Lánský Avatar answered Aug 08 '26 22:08

Lukáš Lánský