Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A better choice if you are using 4.0 is to use a <code>BlockingCollection</code> for that, for older versions use a combination from a <code>Queue&lt;T&gt;</code> and <code>AutoResetEvent</code>. So you will be <strong>notified</strong> when item is added and on the consumer thread and then just consume it. here we are using a push technology where at your current implementation you are using a poll technology "each time you are asking if there is any data".</p> <p>Example: <code>4.0</code></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 "whenever you received an item": _messageBuffer.Add(new Data()); //at the consumer thread "another thread(s) that is running without to consume the data when it arrived." foreach (Data data in _buffer.GetConsumingEnumerable())// or "_buffer.Take" it will block here automatically waiting from new items to be added { //handle the data here. } </code></pre> <p>Example: other "lower" versions:</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>
    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. This table or related slice is empty.
    1. 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