| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SuspendScope.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Utilities.Suspending; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a scope for suspending or allowing operations. When disposed, it will automatically pop itself from the o |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class SuspendScope : IDisposable |
| | | 15 | | { |
| | | 16 | | private readonly Suspender _owner; |
| | | 17 | | private bool _disposed; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="SuspendScope"/> class with the specified owner and suspension state |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="owner">The owner <see cref="Suspender"/> that manages the suspension state.</param> |
| | | 23 | | /// <param name="suspended">A value indicating whether the scope should start in a suspended state.</param> |
| | | 24 | | public SuspendScope(Suspender owner, bool suspended) |
| | | 25 | | { |
| | 81 | 26 | | _owner = owner; |
| | | 27 | | IsSuspended = suspended; |
| | | 28 | | |
| | 81 | 29 | | _owner.Push(this); |
| | 81 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets a value indicating whether operations are currently suspended within this scope. This property reflects the |
| | | 34 | | /// </summary> |
| | | 35 | | public bool IsSuspended { get; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Disposes the scope, which will automatically pop it from the owning <see cref="Suspender"/> to restore the previ |
| | | 39 | | /// </summary> |
| | | 40 | | public void Dispose() |
| | | 41 | | { |
| | 87 | 42 | | if (_disposed) |
| | 3 | 43 | | return; |
| | | 44 | | |
| | 84 | 45 | | _owner.Pop(this); |
| | | 46 | | |
| | 81 | 47 | | _disposed = true; |
| | 81 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |