public synchronized boolean waitForGeneration(long targetGen, int maxMS) throws InterruptedException {if (targetGen > searchingGen) {reopenLock.lock();waitingGen = Math.max(waitingGen, targetGen);try {reopenCond.signal();} finally {reopenLock.unlock();}long startMS = System.nanoTime()/1000000;while (targetGen > searchingGen) {if (maxMS < 0) {wait();} else {long msLeft = (startMS + maxMS) - System.nanoTime()/1000000;if (msLeft <= 0) {return false;} else {wait(msLeft);}}}}return true;}

public virtual bool WaitForGeneration(long targetGen, int maxMS){long curGen = writer.Generation;if (targetGen > curGen){throw new System.ArgumentException("targetGen=" + targetGen + " was never returned by the ReferenceManager instance (current gen=" + curGen + ")");}lock (this)if (targetGen <= searchingGen)return true;else{waitingGen = Math.Max(waitingGen, targetGen);reopenCond.Set();available.Reset();}long startMS = Time.NanoTime() / 1000000;while (targetGen > Interlocked.Read(ref searchingGen)){if (maxMS < 0){available.WaitOne();}else{long msLeft = (startMS + maxMS) - (Time.NanoTime()) / 1000000;if (msLeft <= 0){return false;}else{available.WaitOne(TimeSpan.FromMilliseconds(msLeft));}}}return true;}
