Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Parallel foreach-System.AggregateException:发生了一个或多个错误---&燃气轮机;System.IndexOutOfRangeException_C#_Multithreading_Parallel.foreach - Fatal编程技术网

C#Parallel foreach-System.AggregateException:发生了一个或多个错误---&燃气轮机;System.IndexOutOfRangeException

C#Parallel foreach-System.AggregateException:发生了一个或多个错误---&燃气轮机;System.IndexOutOfRangeException,c#,multithreading,parallel.foreach,C#,Multithreading,Parallel.foreach,找到答案。。。代码中的错误是我将数据添加到了错误的列表中 我知道这已经被问了好几次了,但我就是不明白为什么会出现一个例外,我是不是丢失了一个锁 var sendsToday = new List<TSend>(); var threads = _maxNumberOfThreads; if (threads <= 0) threads = 1; Parallel.ForEach(_subscribers, new ParallelOptions { Max

找到答案。。。代码中的错误是我将数据添加到了错误的列表中


我知道这已经被问了好几次了,但我就是不明白为什么会出现一个例外,我是不是丢失了一个锁

var sendsToday = new List<TSend>();

var threads = _maxNumberOfThreads;
if (threads <= 0)
    threads = 1;

Parallel.ForEach(_subscribers,
    new ParallelOptions { MaxDegreeOfParallelism = threads },
    () => new ContentSendLocalStorage<TSend, TMedium>(_contentServices, _logService),
    (subscriber, loopState, localStorage) =>
    {
        localStorage.LogService.Warning(string.Format("Choosing content for subscriber {0}", subscriber.SubscriberId));
        foreach (var newsletterId in subscriber.SubscribedNewsletterIds)
        {
            localStorage.LogService.Warning(string.Format("Choosing content for newsletter {0}", newsletterId));
            var clicks = StateBag.Get<LookupList>(StateKeys.LookupList).Clicks.Where(c => c.Subscriber.SubscriberId == subscriber.SubscriberId).Select(c => c.Content.ContentId).ToArray();

            foreach (var contentService in _contentServices.Where(contentService => contentService.Contents.Count != 0))
            {
                subscriber.UrlsClicked = contentService.Contents
        .Where(c => clicks.Contains(c.ContentId))
        .GroupBy(g => g.Page.Url)
        .ToDictionary(k => k.Key, v => 1);

                var best = contentService.GetBestForSubscriber(subscriber, new TMedium { MediumId = int.Parse(newsletterId) });
                if (best != null)
        sendsToday.Add(best);
            }
        }

        localStorage.LogService.Warning(string.Format("Done choosing content for subscriber {0}", subscriber.SubscriberId));
        return localStorage;
    },
    finalStorage =>
    {
        lock (ContentSendLock)
        {
            sendsToday.AddRange(finalStorage.SubscriberSends);
        }
    });
var sendsToday=new List();
var threads=\u maxNumberOfThreads;
if(线程新ContentSendLocalStorage(\u contentServices,\u logService),
(订阅服务器、loopState、localStorage)=>
{
localStorage.LogService.Warning(string.Format(“为订阅服务器{0}选择内容”,subscriber.SubscriberId));
foreach(订阅服务器中的var新闻稿ID.SubscribedNewsletteId)
{
localStorage.LogService.Warning(string.Format(“为时事通讯{0}选择内容”,时事通讯ID));
var clicks=StateBag.Get(StateKeys.LookupList).clicks.Where(c=>c.Subscriber.SubscriberId==Subscriber.SubscriberId).选择(c=>c.Content.ContentId).ToArray();
foreach(在_contentServices.Where中的var contentService(contentService=>contentService.Contents.Count!=0))
{
subscriber.UrlsClicked=contentService.Contents
.Where(c=>clicks.Contains(c.ContentId))
.GroupBy(g=>g.Page.Url)
.ToDictionary(k=>k.Key,v=>1);
var best=contentService.GetBestForSubscriber(订阅者,新的TMedium{MediumId=int.Parse(newsletteId)});
如果(最佳!=null)
sendsToday.Add(最佳);
}
}
localStorage.LogService.Warning(string.Format(“为订阅服务器{0}选择内容完成”,subscriber.SubscriberId));
返回本地存储;
},
最终存储=>
{
锁(ContentSendLock)
{
sendsToday.AddRange(finalStorage.SubscriberSends);
}
});
我一直得到以下例外情况:

System.AggregateException: One or more errors occurred. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at WFM.Newsletter.Business.CORE.OptimizationEngine`2.<>c__DisplayClassd.<Start>b__4(Subscriber subscriber, ParallelLoopState loopState, ContentSendLocalStorage`2 localStorage)
   at System.Threading.Tasks.Parallel.<>c__DisplayClass21`2.<ForEachWorker>b__1a(Int32 i, ParallelLoopState state, TLocal local)
   at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
   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.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](TSource[] array, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Func`1 localInit, Func`4 body, Action`1 localFinally)
   at WFM.Newsletter.Business.CORE.OptimizationEngine`2.Start()
   at WFM.Newsletter.Business.CORE.OptimizationService.Start(IEnumerable`1 subscribers, IEnumerable`1 newsletters, LookupList lookupList, LocalConfig config, ILogging logService, Int32 maxNumberOfThreads, String subscriberClicksSourceTableName)
---> (Inner Exception #0) System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at WFM.Newsletter.Business.CORE.OptimizationEngine`2.<>c__DisplayClassd.<Start>b__4(Subscriber subscriber, ParallelLoopState loopState, ContentSendLocalStorage`2 localStorage)
   at System.Threading.Tasks.Parallel.<>c__DisplayClass21`2.<ForEachWorker>b__1a(Int32 i, ParallelLoopState state, TLocal local)
   at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
   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 )<---

---> (Inner Exception #1) System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at WFM.Newsletter.Business.CORE.OptimizationEngine`2.<>c__DisplayClassd.<Start>b__4(Subscriber subscriber, ParallelLoopState loopState, ContentSendLocalStorage`2 localStorage)
   at System.Threading.Tasks.Parallel.<>c__DisplayClass21`2.<ForEachWorker>b__1a(Int32 i, ParallelLoopState state, TLocal local)
   at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
   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 )<---

---> (Inner Exception #2) System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at WFM.Newsletter.Business.CORE.OptimizationEngine`2.<>c__DisplayClassd.<Start>b__4(Subscriber subscriber, ParallelLoopState loopState, ContentSendLocalStorage`2 localStorage)
   at System.Threading.Tasks.Parallel.<>c__DisplayClass21`2.<ForEachWorker>b__1a(Int32 i, ParallelLoopState state, TLocal local)
   at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
   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 )<---
System.AggregateException:发生一个或多个错误。-->System.IndexOutOfRangeException:索引超出了数组的边界。
at System.Collections.Generic.List`1.Add(T项)
在WFM.Newsletter.Business.CORE.OptimizationEngine`2.c_uuDisplayClassD.b_uu4(订户订户订户、ParallelLoopState loopState、ContentSendLocalStorage`2 localStorage)
在System.Threading.Tasks.Parallel.c__;u DisplayClass21`2.b__;1a(Int32 i,ParallelLoopState状态,TLocal local local)
at System.Threading.Tasks.Parallel.c__;u DisplayClassf`1.b__;u c()
在System.Threading.Tasks.Task.InnerInvoke()中
位于System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务)
在System.Threading.Tasks.Task.c\u显示器上显示Class7.b\u 6(对象)
---内部异常堆栈跟踪的结束---
位于System.Threading.Tasks.Task.ThrowifeException(布尔值IncludeTaskCanceledException)
在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken)
在System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive,Int32 toExclusive,ParallelOptions ParallelOptions,Action`1 body,Action`2 bodyWithState,Func`4 bodyWithLocal,Func`1 localInit,Action`1 localFinally)
在System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](TSource[]数组,ParallelOptions ParallelOptions,Action`1 body,Action`2 bodyWithState,Action`3 bodyWithStateAndIndex,Func`4 bodyWithStateAndLocal,Func`5 bodyWithEverything,Func`1 localInit,Action`1 localFinally)
在System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source,ParallelOptions ParallelOptions,Action`1 body,Action`2 bodyWithState,Action`3 bodyWithStateAndIndex,Func`4 bodyWithStateAndLocal,Func`5 bodyWithEverything,Func`1 localInit,Action`1 localFinally)
在System.Threading.Tasks.Parallel.ForEach[TSource,TLocal](IEnumerable`1 source,ParallelOptions,Func`1 localInit,Func`4 body,Action`1 localFinally)
在WFM.Newsletter.Business.CORE.OptimizationEngine`2.Start()上
在WFM.Newsletter.Business.CORE.OptimizationService.Start(IEnumerable`1订户、IEnumerable`1新闻稿、LookupList LookupList、LocalConfig配置、ILogging logService、Int32 maxNumberOfThreads、字符串订阅服务器ClicksSourceTableName)
--->(内部异常#0)System.IndexOutOfRangeException:索引超出了数组的边界。
at System.Collections.Generic.List`1.Add(T项)
在WFM.Newsletter.Business.CORE.OptimizationEngine`2.c_uuDisplayClassD.b_uu4(订户订户订户、ParallelLoopState loopState、ContentSendLocalStorage`2 localStorage)
在System.Threading.Tasks.Parallel.c__;u DisplayClass21`2.b__;1a(Int32 i,ParallelLoopState状态,TLocal local local)
at System.Threading.Tasks.Parallel.c__;u DisplayClassf`1.b__;u c()
在System.Threading.Tasks.Task.InnerInvoke()中
位于System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务)
在System.Threading.Tasks.Task.c_DisplayClass7.b__6(对象)(内部异常#1)System.IndexOutOfRangeException:索引超出了数组的边界。
at System.Collections.Generic.List`1.Add(T项)
在WFM.Newsletter.Business.CORE.OptimizationEngine`2.c_uuDisplayClassD.b_uu4(订户订户订户、ParallelLoopState loopState、ContentSendLocalStorage`2 localStorage)
在System.Threading.Tasks.Parallel.c__;u DisplayClass21`2.b__;1a(Int32 i,ParallelLoopState状态,TLocal local local)
at System.Threading.Tasks.Parallel.c__;u DisplayClassf`1.b__;u c()
在System.Threading.Tasks.Task.InnerInvoke()中
位于System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务)
在System.Threading.Tasks.Task.c_DisplayClass7.b__6(对象)(内部异常#2)System.IndexOutOfRangeException:索引超出了数组的边界。
at System.Collections.Generic.List`1.Add(T项)
在WFM.Newsletter.Business.CORE.OptimizationEngine`2.c_uuDisplayClassD.b_uu4(订户订户订户、ParallelLoopState loopState、ContentSendLocalStorage`2 localStorage)
在System.Threading.Tasks.Parallel.c__DisplayClass21`2.b__1a(Int32 i,ParallelLoopState,TLocal loc
if (best != null)
    sendsToday.Add(best);
}
var sendsToday = new ConcurrentBag<TSend>();
sendsToday.Add(best);
 if (best != null)
        sendsToday.Add(best);
if (best != null)
localStorage.SubscriberSends.Add(best);