0

実行が完了する前に接続を閉じる並列 foreach に問題があります。通常の foreach ループを実行すると、速度は遅くなりましたが、すべてが返されました。並列 foreach に変更すると、データの約 95% が返されて終了します。

以下は私が使用しているコードです:

var USPostalCodes = repository.GetUSPostalCodes();
                var CAPostalCodes = repository.GetCAPostalCodes();

                Parallel.ForEach(spreadsheetinfo, location =>
                {

                    LocationData Locationdata = new LocationData()
                    {
                        id = location.Id,
                        Market = repository.GetMarketsForPostalCode(location.PostalCode, uploadedFile, USPostalCodes, CAPostalCodes),
                    };

                    locationlist.Add(Locationdata);
                });

I added the following code to check and see what was going on, and that fixed it so that it is returning all rows so i know that a race condition exists but what i can't figure out is why and how ot fix it.  any suggestions would be greatly appreciated

Console.WriteLine("Processing {0} on thread {1}", Locationdata,
                                    Thread.CurrentThread.ManagedThreadId);
4

1 に答える 1

2

locationlistおそらくスレッドセーフではありません。したがって、リストを破損しています。

代わりに、 を使用.AsParrelel.Select()してデリゲートを並行して実行IEnumerable<T>し、結果とともに を返す必要があります。

于 2013-04-05T17:45:41.183 に答える