ReentrancyGuard |
A utility class which helps guard against reentrancy. This is safer and cleaner alternative to the typical pattern of adding a bool flag to a class, and setting it to true when you wish to guard, then resetting it back to false. The ReentrancyGuard returns an IDisposable token when call its Set method. From this point on, its IsSet property will be true. When you wish to exit out of the safe block, call Dispose on the token. The best usage is within a using block: // class member: private readonly ReentrancyGuard guard; // to protect a block from reentrancy: using (this.guard.Set()) { ... safe code } And wherever you need to guard: if (this.guard.IsSet) { return; } This class is meant to be used only a single thread. |