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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With