我不明白为什么我今天收到了System.IndexOutOfRangeException
。当程序已经运行了 8 个小时并且发生异常的地方被执行了数百万次时,我已经收到了它。这个地方很简单:
for (int i = 0; i < _goldDesiredOrdersBuy.Length; i++)
{
_goldDesiredOrdersBuy[i] = -1; // IndexOutOfRangeException! Strategy3.cs:line 666
}
我仅在程序启动时初始化 _goldDesiredOrdersBuy 一次,因此可以保证在发生异常时对其进行初始化:
private int[] _goldDesiredOrdersBuy = new int[MaxOrderbookDepth];
我有另一个地方可以触摸这个数组:
private int GetGoldVolumeBuy(int bidQuotesPos)
{
if (_goldDesiredOrdersBuy[bidQuotesPos] > -1)
{
return _goldDesiredOrdersBuy[bidQuotesPos];
}
int result = GetGoldVolumeBuyNotCached(bidQuotesPos);
_goldDesiredOrdersBuy[bidQuotesPos] = result;
return result;
}
就这样。_goldDesiredOrdersBuy
应用程序启动时初始化一次,保证被初始化并且数组的长度不会在任何地方修改,所以我不明白我是如何收到IndexOutOfRangeException
的,有什么想法吗?
Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. --->
System.AggregateException: One or more errors occurred. --->
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at MyProj.Strategies.Strategy3.CalculateNewDesiredOrdersBuy() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 666
at MyProj.Strategies.Strategy3.RecalculateBuyOrders() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 567
at MyProj.Strategies.Strategy3.OnAllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 499
at MyProj.Strategies.Strategy3.AllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 413
at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
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.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
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](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](IEnumerable`1 source, Action`1 body)
at MyProj.Market.FinishUpdatingTables() in C:\Oleg\projects\MyProj\MyProj\Market.cs:line 449
at Library.Exchange.Gate.DoGateIteration() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 143
at Library.Exchange.Gate.<Connect>b__0() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 98
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
at System.Threading.Tasks.TaskExceptionHolder.Finalize()