Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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# 添加多个值时出现排队错误_C# - Fatal编程技术网

C# 添加多个值时出现排队错误

C# 添加多个值时出现排队错误,c#,C#,我正在尝试将值排队到一个队列中,它工作正常。当程序运行几个小时时,我得到以下错误 System.ArgumentException: length at System.Array.Copy (System.Array sourceArray, System.Int32 sourceIndex, System.Array destinationArray, System.Int32 destinationIndex, System.Int32 length) [0x000c3] in <

我正在尝试将值排队到一个队列中,它工作正常。当程序运行几个小时时,我得到以下错误

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 
System.ArgumentException:长度
在System.Array.Copy(System.Array sourceArray、System.Int32 sourceIndex、System.Array destinationArray、System.Int32 destinationIndex、System.Int32 length)[0x000c3]中:0
位于System.Collections.Generic.Queue`1[T].SetCapacity(System.Int32 capacity)[0x0001e]中的:0
位于System.Collections.Generic.Queue`1[T]。在以下位置排队(T项)[0x0003e]:0
在:0中的VSCaptureMP.MPudpclient.ExportWaveToCSV()[0x0010a]处
在:0中的VSCaptureMP.MPudpclient.PollPacketDecoder(System.Byte[]packetbuffer,System.Int32 headersize)[0x00121]处
我每100毫秒排队一次值,我有一个任务从队列中退出。队列没有增加,其计数介于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
at(wrapper alloc)System.Object.AllocVector(intptr,intptr)
在System.Collections.Concurrent.ConcurrentQueue1+段[T]…ctor(System.Int32 boundedLength)[0x00006]中:0
位于:0中的System.Collections.Concurrent.ConcurrentQueue1[T].EnqueueSlow(T项)[0x00051]处
在System.Collections.Concurrent.ConcurrentQueue`1[T].Enqueue(T项)[0x00010]中:0

根据您的描述,我认为您的潜在问题可能是由竞争条件引起的。像列表这样的队列在内部使用数组,并在需要时调整它们的大小。它们以2的幂进行运算,例如128、256和512,这与您的计数边界重合。调整大小意味着创建一个新数组(更小或更大),并将内容复制到新数组中,然后调用
array.Copy()

从Callstack中,我可以看到队列在
排队
操作期间被调整了大小。显然,在此同时,出列操作已经收缩了内部数组,导致参数异常。尝试改用a,然后再次检查


编辑:链接到最新文档。

发布您的代码!即使在紧密循环中添加条目,队列也不会抛出。500是一小部分数据。另一方面,
队列
不是线程安全的,不应该被多线程修改。我也尝试了并发队列。获取了类似错误:在System.Collections.Concurrent.ConcurrentQueue
1+段[T]…ctor(System.Int32 boundedLength)[0x00006]中:0在System.Collections.Concurrent.ConcurrentQueue
1[T]。EnqueueSlow(T项)[0x00051]in:0位于System.Collections.Concurrent.ConcurrentQueue`1[T]。Enqueue(T项)[0x00010]位于:0开始变得有趣。你能把这个(包括例外信息)添加到你原来的post plz吗?当然。我已经更新了问题。感谢您的回复您能在Stacktrace中包含异常类型吗?