using System.Collections.Concurrent; namespace Terminal.Gui.App; /// Defines a session token for a running . public class SessionToken : IDisposable { /// Initializes a new class. /// public SessionToken (Toplevel view) { Toplevel = view; } /// The belonging to this . public Toplevel Toplevel { 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 (Toplevel is { } && disposing) { // Previously we were requiring Toplevel be disposed here. // But that is not correct becaue `Begin` didn't create the TopLevel, `Init` did; thus // disposing should be done by `Shutdown`, not `End`. throw new InvalidOperationException ( "Toplevel must be null before calling Application.SessionToken.Dispose" ); } } #if DEBUG_IDISPOSABLE #pragma warning disable CS0419 // Ambiguous reference in cref attribute /// /// Gets whether was called on this SessionToken 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; } = 0; /// /// Gets the list of SessionToken objects that have been created and not yet disposed. /// Note, this is a static property and will affect all SessionToken objects. /// For debug purposes to verify objects are being disposed properly. /// Only valid when DEBUG_IDISPOSABLE is defined. /// public static ConcurrentBag Instances { get; private set; } = []; /// Creates a new SessionToken object. public SessionToken () { Instances.Add (this); } #pragma warning restore CS0419 // Ambiguous reference in cref attribute #endif }