/// <summary> /// Creates this instance. /// </summary> /// <returns>Create new Disposable.</returns> public IDisposable Create() { if (Interlocked.Increment(ref this.count) == 1) { this.state = DeferState.Deferred; } return(new Disposable(this.Decrement)); }
/// <summary> /// Decrements this instance. /// </summary> private void Decrement() { if (Interlocked.Decrement(ref this.count) == 0) { if (this.state == DeferState.Update) { this.update(); } this.state = DeferState.NotDeferred; } }
/// <summary> /// Updates this instance. /// </summary> /// <returns>Is value updated [True] or defereed [False].</returns> public bool Update() { switch (this.state) { case DeferState.Update: return(false); case DeferState.Deferred: this.state = DeferState.Update; return(false); default: this.update(); return(true); } }