Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is not because of the <code>BlockingCollection</code> or <code>ConcurrentQueue</code>, but the while loop:</p> <pre><code>while(socket.Connected) { while (!listOfQueueItems.IsEmpty) { /*code*/ } } </code></pre> <p>Of course it will take the cpu down; because of if the queue is empty, then the while loop is just like:</p> <pre><code>while (true) ; </code></pre> <p>which in turn will eat the cpu resources.</p> <p>This is not a good way of using <code>ConcurrentQueue</code> you should use <code>AutoResetEvent</code> with it so whenever item is added you will be notified. Example:</p> <pre><code>private ConcurrentQueue&lt;Data&gt; _queue = new ConcurrentQueue&lt;Data&gt;(); private AutoResetEvent _queueNotifier = new AutoResetEvent(false); //at the producer: _queue.Enqueue(new Data()); _queueNotifier.Set(); //at the consumer: while (true)//or some condition { _queueNotifier.WaitOne();//here we will block until receive signal notification. Data data; if (_queue.TryDequeue(out data)) { //handle the data } } </code></pre> <p>For a good usage of the <code>BlockingCollection</code> you should use the <code>GetConsumingEnumerable()</code> to wait for the items to be added, Like:</p> <pre><code>//declare the buffer private BlockingCollection&lt;Data&gt; _buffer = new BlockingCollection&lt;Data&gt;(new ConcurrentQueue&lt;Data&gt;()); //at the producer method: _messageBuffer.Add(new Data()); //at the consumer foreach (Data data in _buffer.GetConsumingEnumerable())//it will block here automatically waiting from new items to be added and it will not take cpu down { //handle the data here. } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload