1

特定の時点の値をタスクに渡す、または凍結する方法は?

以下のコードの問題は、DocFTSinXsCollection(SID) に渡される SID は、次の rdr.Read() からの SID ではなく、Task.Factory.StartNew が実行されたときではありません。

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
}
4

1 に答える 1

1

各クロージャーが独自の変数を取得できるようSIDに、ループ内でローカル変数として宣言する必要があります。while

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
}
于 2012-09-02T01:11:55.367 に答える