RunState.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// The execution state for a <see cref="Toplevel"/> view.
  6. /// </summary>
  7. public class RunState : IDisposable {
  8. /// <summary>
  9. /// Initializes a new <see cref="RunState"/> class.
  10. /// </summary>
  11. /// <param name="view"></param>
  12. public RunState (Toplevel view)
  13. {
  14. Toplevel = view;
  15. }
  16. /// <summary>
  17. /// The <see cref="Toplevel"/> belonging to this <see cref="RunState"/>.
  18. /// </summary>
  19. public Toplevel Toplevel { get; internal set; }
  20. #if DEBUG_IDISPOSABLE
  21. /// <summary>
  22. /// For debug (see DEBUG_IDISPOSABLE define) purposes to verify objects are being disposed properly
  23. /// </summary>
  24. public bool WasDisposed = false;
  25. /// <summary>
  26. /// For debug (see DEBUG_IDISPOSABLE define) purposes to verify objects are being disposed properly
  27. /// </summary>
  28. public int DisposedCount = 0;
  29. /// <summary>
  30. /// For debug (see DEBUG_IDISPOSABLE define) purposes; the runstate instances that have been created
  31. /// </summary>
  32. public static List<RunState> Instances = new List<RunState> ();
  33. /// <summary>
  34. /// Creates a new RunState object.
  35. /// </summary>
  36. public RunState ()
  37. {
  38. Instances.Add (this);
  39. }
  40. #endif
  41. /// <summary>
  42. /// Releases all resource used by the <see cref="RunState"/> object.
  43. /// </summary>
  44. /// <remarks>
  45. /// Call <see cref="Dispose()"/> when you are finished using the <see cref="RunState"/>.
  46. /// </remarks>
  47. /// <remarks>
  48. /// <see cref="Dispose()"/> method leaves the <see cref="RunState"/> in an unusable state. After
  49. /// calling <see cref="Dispose()"/>, you must release all references to the
  50. /// <see cref="RunState"/> so the garbage collector can reclaim the memory that the
  51. /// <see cref="RunState"/> was occupying.
  52. /// </remarks>
  53. public void Dispose ()
  54. {
  55. Dispose (true);
  56. GC.SuppressFinalize (this);
  57. #if DEBUG_IDISPOSABLE
  58. WasDisposed = true;
  59. #endif
  60. }
  61. /// <summary>
  62. /// Releases all resource used by the <see cref="RunState"/> object.
  63. /// </summary>
  64. /// <param name="disposing">If set to <see langword="true"/> we are disposing and should dispose held objects.</param>
  65. protected virtual void Dispose (bool disposing)
  66. {
  67. if (Toplevel != null && disposing) {
  68. throw new InvalidOperationException ("You must clean up (Dispose) the Toplevel before calling Application.RunState.Dispose");
  69. }
  70. }
  71. }