paparazzo
paparazzo

Reputation: 45106

How to pass or freeze a point in time value to a task?

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
}

Upvotes: 1

Views: 37

Answers (1)

SLaks
SLaks

Reputation: 888223

You need to declare SID as a local variable within the while loop so that each closure will get its own variable:

while (rdr.Read())
{
     int 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
}

Upvotes: 1

Related Questions