Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <em>best</em> approach is to have some mechanism to <strong>actively detect</strong> the thing becoming true (rather than passively polling for it having <em>become</em> true); this could be any kind of wait-handle, or maybe a <code>Task</code> with <code>Wait</code>, or maybe an <code>event</code> that you can subscribe to to unstick yourself. Of course, if you do that kind of "wait until something happens", that is <strong>still</strong> not as efficient as simply having the next bit of work done <em>as a callback</em>, meaning: you don't need to use a thread to wait. <code>Task</code> has <code>ContinueWith</code> for this, or you can just do the work in an <code>event</code> when it gets fired. The <code>event</code> is probably the simplest approach, depending on the context. <code>Task</code>, however, already provides most-everything you are talking about here, including both "wait with timeout" and "callback" mechanisms.</p> <p>And yes, spinning for 10 seconds is not great. If you want to use something like your current code, and if you have reason to expect a short delay, but need to allow for a longer one - maybe <code>SpinWait</code> for (say) 20ms, and use <code>Sleep</code> for the rest?</p> <hr> <p>Re the comment; here's how I'd hook an "is it full" mechanism:</p> <pre><code>private readonly object syncLock = new object(); public bool WaitUntilFull(int timeout) { if(CollectionIsFull) return true; // I'm assuming we can call this safely lock(syncLock) { if(CollectionIsFull) return true; return Monitor.Wait(syncLock, timeout); } } </code></pre> <p>with, in the "put back into the collection" code:</p> <pre><code>if(CollectionIsFull) { lock(syncLock) { if(CollectionIsFull) { // double-check with the lock Monitor.PulseAll(syncLock); } } } </code></pre>
 

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