さまざまなフィードのURLを含むテキストファイルがあります。次のコードを使用して、コレクション(IEnumerable)内のすべてのURLを読み取っています。
var URLs = File.ReadLines(Path.GetFullPath(@"Resources\FeedList.txt"));
次の行では、合計数を印刷しています:
Console.WriteLine("Total Number of feeds : {0}",URLs.Count());
その後、Parellel.ForEach構文を使用して、各URLに対応するロジックを実行します。以下はコードです、私は使用しています:
Parallel.ForEach(URLs, (url) =>
{
// Some business logic
});
問題は、URLの数を出力するコード、つまりURLオブジェクトのCount()メソッドを呼び出すコードを追加するとすぐに、次の例外が発生することです。例外 :
Total Number of feeds : 78
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.ObjectDisposedException: Cannot read from a closed TextReader.
at System.IO.__Error.ReaderClosed()
at System.IO.StreamReader.ReadLine()
at System.IO.File.<InternalReadLines>d__0.MoveNext()
at System.Collections.Concurrent.Partitioner.DynamicPartitionerForIEnumerable`1.InternalPartitionEnumerator.GrabNextChunk(Int32 requestedChunkSize)
at System.Collections.Concurrent.Partitioner.DynamicPartitionEnumerator_Abstract`2.MoveNext()
at System.Threading.Tasks.Parallel.<>c__DisplayClass32`2.<PartitionerForEachWorker>b__30()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass7.<ExecuteSelfReplicating>b__6(Object )
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWi
at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3
at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
at DiscoveringGroups.Program.Main(String[] args) in C:\Users\Pawan Mishra\Documents\Visual Studio 2010\Projects\ProgrammingCollectiveIntelligence\DiscoveringGroups\Pro
Press any key to continue . . .
また、カウント値を出力する行を削除/コメントアウトすると、Parallel.ForEachループは正常に実行されます。
ここで何がうまくいかないのか誰かが何か考えを持っていますか?