Process.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using static MiniTerm.Native.ProcessApi;
  4. namespace MiniTerm
  5. {
  6. /// <summary>
  7. /// Represents an instance of a process.
  8. /// </summary>
  9. internal sealed class Process : IDisposable
  10. {
  11. public Process(STARTUPINFOEX startupInfo, PROCESS_INFORMATION processInfo)
  12. {
  13. StartupInfo = startupInfo;
  14. ProcessInfo = processInfo;
  15. }
  16. public STARTUPINFOEX StartupInfo { get; }
  17. public PROCESS_INFORMATION ProcessInfo { get; }
  18. #region IDisposable Support
  19. private bool disposedValue = false; // To detect redundant calls
  20. void Dispose(bool disposing)
  21. {
  22. if (!disposedValue)
  23. {
  24. if (disposing)
  25. {
  26. // dispose managed state (managed objects).
  27. }
  28. // dispose unmanaged state
  29. // Free the attribute list
  30. if (StartupInfo.lpAttributeList != IntPtr.Zero)
  31. {
  32. DeleteProcThreadAttributeList(StartupInfo.lpAttributeList);
  33. Marshal.FreeHGlobal(StartupInfo.lpAttributeList);
  34. }
  35. // Close process and thread handles
  36. if (ProcessInfo.hProcess != IntPtr.Zero)
  37. {
  38. CloseHandle(ProcessInfo.hProcess);
  39. }
  40. if (ProcessInfo.hThread != IntPtr.Zero)
  41. {
  42. CloseHandle(ProcessInfo.hThread);
  43. }
  44. disposedValue = true;
  45. }
  46. }
  47. ~Process()
  48. {
  49. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  50. Dispose(false);
  51. }
  52. // This code added to correctly implement the disposable pattern.
  53. public void Dispose()
  54. {
  55. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  56. Dispose(true);
  57. // use the following line if the finalizer is overridden above.
  58. GC.SuppressFinalize(this);
  59. }
  60. #endregion
  61. }
  62. }