Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you need here are condition variables. I don't know specifically for .NET but in general you will have something like this</p> <pre><code>Condition cond; Mutex lock; public Cancel() { lock.lock() _cancelled = true; cond.signal(lock); lock.unlock(); } public Weight Resolve(){ _cancelled = false; lock.lock(); while(_source.CurrentWeight &lt; threshold) { if(_cancelled) { lock.unlock(); throw new CancelledOperationException(); } cond.timedWait(lock, 100); // Wait until one of the above conditions is met } lock.unlock(); return _source.CurrentWeight } </code></pre> <p>even better would be if your WeightResolver signalled on the same condition when the weight changed. e.g.</p> <pre><code>Condition cond; Mutex lock; public Cancel() { lock.lock() _cancelled = true; cond.signal(lock); lock.unlock(); } public Weight Resolve(){ _cancelled = false; lock.lock(); while(_source.CurrentWeight &lt; threshold) { if(_cancelled) { lock.unlock(); throw new CancelledOperationException(); } cond.wait(lock); // Wait until one of the above conditions is met } lock.unlock(); return _source.CurrentWeight } </code></pre> <p>and in the WeightMonitor Class, you had something like this.</p> <pre><code>public void updateWeight() { lock.lock(); ...update weight; cond.signal(lock); lock.unlock(); } </code></pre> <p>where the Conditionvariable and lock are the same. in both classes.</p> <p>This is a pretty standard use for condition variables, this is also how join is generally implemented.</p>
 

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