How to pass or freeze a point in time value to a task?
The problem the code below is the SID passed to DocFTSinXsCollection(SID) is NOT when Task.Factory.StartNew was executed rather the SID from the next rdr.Read().
while (rdr.Read())
{
SID = rdr.GetInt32(0);
// other code
Task.Factory.StartNew(() =>
{
// this often gets the new SID - I need the SID from when Task was started
DocFTSinXsCollection docFTSinXsCollection = new DocFTSinXsCollection(SID);
}
// other code
}
You need to declare
SIDas a local variable within thewhileloop so that each closure will get its own variable: