Created
August 4, 2021 01:47
-
-
Save neuecc/1dff9516cb23a5bd233e252d0261bfe4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SynchronizedEnumerator<T> : IEnumerator<T> | |
| { | |
| bool isDisposed; | |
| readonly object gate; | |
| readonly bool lockTaken; | |
| readonly IEnumerator<T> enumerator; | |
| public SynchronizedEnumerator(object gate, IEnumerator<T> enumerator) | |
| { | |
| this.gate = gate; | |
| this.enumerator = enumerator; | |
| Monitor.Enter(gate, ref lockTaken); | |
| } | |
| public T Current => enumerator.Current; | |
| object IEnumerator.Current => Current!; | |
| public bool MoveNext() => enumerator.MoveNext(); | |
| public void Reset() => enumerator.Reset(); | |
| public void Dispose() | |
| { | |
| if (!isDisposed) | |
| { | |
| isDisposed = true; | |
| try | |
| { | |
| enumerator.Dispose(); | |
| } | |
| finally | |
| { | |
| if (lockTaken) | |
| { | |
| Monitor.Exit(gate); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment