我正在尝试将值排入队列并且它工作正常。当程序运行几个小时后,出现以下错误
System.ArgumentException: length
at System.Array.Copy (System.Array sourceArray, System.Int32 sourceIndex, System.Array destinationArray, System.Int32 destinationIndex, System.Int32 length) [0x000c3] in <f56c876907e742b0aa586f051fcce845>:0
at System.Collections.Generic.Queue`1[T].SetCapacity (System.Int32 capacity) [0x0001e] in <ccafeb0e74bd436bb84e5138772c2bb0>:0
at System.Collections.Generic.Queue`1[T].Enqueue (T item) [0x0003e] in <ccafeb0e74bd436bb84e5138772c2bb0>:0
at VSCaptureMP.MPudpclient.ExportWaveToCSV () [0x0010a] in <f1c552d4f5b3424d9438ec1100580a9d>:0
at VSCaptureMP.MPudpclient.PollPacketDecoder (System.Byte[] packetbuffer, System.Int32 headersize) [0x00121] in <f1c552d4f5b3424d9438ec1100580a9d>:0
我每百毫秒对值进行一次排队,并且我有一个从队列中出队的任务。队列没有增加,其数量在 250 到 500 之间。可能是什么问题?我也尝试了 ConcurrentQueue。该程序随后运行了更长的时间,但在 12 小时后出现以下异常:
at (wrapper alloc) System.Object.AllocVector(intptr,intptr)
at System.Collections.Concurrent.ConcurrentQueue1+Segment[T]..ctor (System.Int32 boundedLength) [0x00006] in <f56c876907e742b0aa586f051fcce845>:0
at System.Collections.Concurrent.ConcurrentQueue1[T].EnqueueSlow (T item) [0x00051] in <f56c876907e742b0aa586f051fcce845>:0
at System.Collections.Concurrent.ConcurrentQueue`1[T].Enqueue (T item) [0x00010] in <f56c876907e742b0aa586f051fcce845>:0
最佳答案
根据您的描述,我认为您的潜在问题可能是由竞争条件引起的。像列表这样的队列在内部使用数组并在需要时调整它们的大小。他们以 2 的幂来做,例如128、256 和 512,恰好与您的计数边界对齐。调整大小意味着创建一个新数组(更小或更大)并将内容复制到新数组,传递 Array.Copy()
调用。
从 Callstack 中,我可以看到在 Enqueue
操作期间调整了队列的大小。显然与此同时,出队操作已经缩小了导致参数异常的内部数组。尝试使用 ConcurrentQueue而是再次检查。
编辑最新文档的链接。
关于c# - 添加多个值时出现排队错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50129243/