Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :)

C#
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");
 }    
}


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.
Posted
Updated 22-Feb-16 18:12pm

1 solution

Maybe the problem is when you enqueue the items. Maybe you enqueue references to the same object.


For instance, look at the following code:


C#
class MyClass
{
    public int a;
}

C#
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);

After running the code, you can see that c1 is equal to c2...

 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900