using System.Collections.Concurrent; namespace Terminal.Gui.App; /// /// Represents a running session created by . /// Wraps an instance and is stored in . /// public class RunnableSessionToken : IDisposable { internal RunnableSessionToken (IRunnable runnable) { Runnable = runnable; } /// /// Gets or sets the runnable associated with this session. /// Set to by when the session completes. /// public IRunnable? Runnable { get; internal set; } /// /// Releases all resource used by the object. /// /// /// /// Call when you are finished using the . /// /// /// method leaves the in an unusable state. After /// calling /// , you must release all references to the so the /// garbage collector can /// reclaim the memory that the was occupying. /// /// public void Dispose () { Dispose (true); GC.SuppressFinalize (this); #if DEBUG_IDISPOSABLE WasDisposed = true; #endif } /// /// Releases all resource used by the object. /// /// If set to we are disposing and should dispose held objects. protected virtual void Dispose (bool disposing) { if (Runnable is { } && disposing) { // Runnable must be null before disposing throw new InvalidOperationException ( "Runnable must be null before calling RunnableSessionToken.Dispose" ); } } #if DEBUG_IDISPOSABLE #pragma warning disable CS0419 // Ambiguous reference in cref attribute /// /// Gets whether was called on this RunnableSessionToken or not. /// For debug purposes to verify objects are being disposed properly. /// Only valid when DEBUG_IDISPOSABLE is defined. /// public bool WasDisposed { get; private set; } /// /// Gets the number of times was called on this object. /// For debug purposes to verify objects are being disposed properly. /// Only valid when DEBUG_IDISPOSABLE is defined. /// public int DisposedCount { get; private set; } /// /// Gets the list of RunnableSessionToken objects that have been created and not yet disposed. /// Note, this is a static property and will affect all RunnableSessionToken objects. /// For debug purposes to verify objects are being disposed properly. /// Only valid when DEBUG_IDISPOSABLE is defined. /// public static ConcurrentBag Instances { get; } = []; /// Creates a new RunnableSessionToken object. public RunnableSessionToken () { Instances.Add (this); } #pragma warning restore CS0419 // Ambiguous reference in cref attribute #endif }