I have the following method :
private void GetHistoricalRawDataFromTextFiles(MessageHeader header, HistoricalRawDataFromTextFileSubscriptionDto dto)
{
var computeInstance = GetComputeInstance(dto.SubscriberId, dto.ComputeInstanceId);
var task = computeInstance.GetHistoricalRawDataFromTextFiles(dto, progress => SendProgress(header.Sender, progress));
task.ContinueWith(myTask =>
{
dto.TimeSeries = myTask.Result;
Messenger.SendTo(SubscriberId, header.Sender, MessageType.Reply, MessageTopic.HistoricalRawDataFromTextFiles, dto);
});
}
Method computeInstance.GetHistoricalRawDataFromTextFiles
returns a Task<List<string>>
and my question is
header
and dto
into the lambda expression and task continuations. It is important that the header
and dto
instance values are captured within the lambda expression and task continuation at the time the outer method is called. The same method may be called again before the task from the previous call completes.It is important that the header and dto instance values are captured within the lambda expression and task continuation at the time the outer method is called.
When using lambda expressions, what gets closed over is the variable, and not the value of that variable.
As long as header
and dto
aren't global variables which you modify each time before making the method call, you should be fine. If they are global variables, then you'll need to find a way to create a local copy for each of them. If they're reference types, you'll need to clone them, and if they're value types, you'll need to copy them to a local variable inside the lambda.