2012-10-30 35 views
6

我運行一個查詢PLINQ如下:PLINQ查詢給溢出異常

ParallelQuery<string> winningCombos = from n in nextComboMaker.GetNextCombo() 
               .AsParallel().WithCancellation(_cancelSource.Token) 
               where ComboWasAWinner(n) 
               select n; 

ConcurrentBag<string> wins = new ConcurrentBag<string>(); 

foreach (var winningCombo in winningCombos) 
{ 
     wins.Add(winningCombo); 
     if (wins.Count == _maxWinsAllowed) 
      break; 
} 

的GetNextCombo方法只是返回的字母和數字的組合旁邊,有可能高達數十億美元/兆。

這是現在拋出異常時,我選擇的範圍比一個Int32允許的大小大的組合,它總是拋出時,它的運行連擊計數器是2147483584.

我確信它是什麼通過創建一個假的組合返回每次GetNextCombo(做一個回報收益率「234gf24fa23 ......」等)

的異常正被拋出LINQ:

System.AggregateException was unhandled by user code 
    Message=One or more errors occurred. 
    Source=System.Core 
    StackTrace: 
     at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose) 
     at System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId) 
     at System.Linq.Parallel.PartitionedStreamMerger`1.Receive[TKey](PartitionedStream`2 partitionedStream) 
     at System.Linq.Parallel.ForAllOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings) 
     at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream) 
     at System.Linq.Parallel.WhereQueryOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings) 
     at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream) 
     at System.Linq.Parallel.ScanQueryOperator`1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient) 
     at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient) 
     at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient) 
     at System.Linq.Parallel.QueryOperator`1.GetOpenedEnumerator(Nullable`1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) 
     at System.Linq.Parallel.ForAllOperator`1.RunSynchronously() 
     at StockWiz.Library.PLINQArrayProcessor.DoProcessing() in C:\Users\dad\Documents\BitBucket\stockwiz_clone\stockwiz\StockWiz.Library\PLINQArrayProcessor.cs:line 50 
     at System.Threading.Tasks.Task.Execute() 
    InnerException: System.OverflowException 
     Message=Arithmetic operation resulted in an overflow. 
     Source=System.Core 
     StackTrace: 
      at System.Linq.Parallel.PartitionedDataSource`1.ContiguousChunkLazyEnumerator.MoveNext(T& currentElement, Int32& currentKey) 
      at System.Linq.Parallel.WhereQueryOperator`1.WhereQueryOperatorEnumerator`1.MoveNext(TInputOutput& currentElement, TKey& currentKey) 
      at System.Linq.Parallel.ForAllOperator`1.ForAllEnumerator`1.MoveNext(TInput& currentElement, Int32& currentKey) 
      at System.Linq.Parallel.ForAllSpoolingTask`2.SpoolingWork() 
      at System.Linq.Parallel.SpoolingTaskBase.Work() 
      at System.Linq.Parallel.QueryTask.BaseWork(Object unused) 
      at System.Threading.Tasks.Task.Execute() 
     InnerException: 

我想知道如果有什麼我可以做改變這個問題y以不溢出,在我做事情的任何命令,等等也許沒有LINQ查詢做了,其中,選擇,雖然我已經試過這樣:

var query = nextComboMaker.GetNextCombo().AsParallel(); 

query.ForAll(x => if(ComboWasAWinner(x) wins.Add(x)); 

仍然是相同的溢出。

回答

6

您應該可以通過使用自定義分區程序解決此問題。在這種情況下,由PLINQ選擇的默認分區程序僅支持整數範圍的項目。

有關如何操作,請參閱http://msdn.microsoft.com/en-us/library/dd997416.aspx

+0

似乎不適合我,你必須對IEnumerable執行.ToArray或ToList()我平行過來,並且一旦尺寸大於int就會造成堆棧溢出。我可以重寫哪些具體內容以使其與長期計數? –