问题描述
嗨
有人可以帮我理解我的问题吗?
我已经宣布了一个并发队列,当我加入队列时,一切似乎都很好。
但是当我排队时,它似乎是多次复制相同的对象。
我做错了什么:)
Hi
Can someown help me understand my problem?
I have declare a Concurrent Queue and when i enqueus to the queue, everything seems allright.
But when i de-queue it seems like its the same object being copied multiple times.
What am i doing wrong :)
private void OnDequeue()
{
try
{
List<MicronOptics.Hyperion.Communication.PeakData> items = new List<MicronOptics.Hyperion.Communication.PeakData>();
MicronOptics.Hyperion.Communication.PeakData item;
var count = this.collection.Count();
while (items.Count() != count)
{
if (this.collection.TryDequeue(out item))
items.Add(item);
}
}
catch (Exception)
{
Trace.TraceError("Unexpected error occured doing dequeue");
}
}
我的尝试:
我试图在排队语法附近插入一个断点,以确保问题在排队之前不是问题。
Inside我的PeakData对象我有一个增加的int值,它正在增加,所以我的结论是我的出列问题。
What I have tried:
I have try to insert a break point near the enqueue syntax, to make sure the problem isn't already an issue before its in the queue.
Inside my PeakData object i have an increasing int value, it is increasing, so my conclusion is that it is a problem with my dequeuing.
推荐答案
例如,请查看以下代码:
For instance, look at the following code:
class MyClass
{
public int a;
}
MyClass c = new MyClass();
ConcurrentQueue<MyClass> cq = new ConcurrentQueue<MyClass>();
c.a = 1;
cq.Enqueue(c);
c.a = 2;
cq.Enqueue(c);
MyClass c1;
MyClass c2;
cq.TryDequeue(out c1);
cq.TryDequeue(out c2);
运行代码后,你可以看到 c1
等于 c2
...
After running the code, you can see that c1
is equal to c2
...
这篇关于C#concurrentqueue dequeue问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!