ProcessExtensions.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #nullable enable
  2. using System;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.Common.Extensions
  7. {
  8. /// <summary>
  9. /// Extension methods for <see cref="Process"/>.
  10. /// </summary>
  11. public static class ProcessExtensions
  12. {
  13. /// <summary>
  14. /// Asynchronously wait for the process to exit.
  15. /// </summary>
  16. /// <param name="process">The process to wait for.</param>
  17. /// <param name="timeout">The duration to wait before cancelling waiting for the task.</param>
  18. /// <returns>True if the task exited normally, false if the timeout elapsed before the process exited.</returns>
  19. /// <exception cref="InvalidOperationException">If <see cref="Process.EnableRaisingEvents"/> is not set to true for the process.</exception>
  20. public static async Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout)
  21. {
  22. using (var cancelTokenSource = new CancellationTokenSource(timeout))
  23. {
  24. return await WaitForExitAsync(process, cancelTokenSource.Token).ConfigureAwait(false);
  25. }
  26. }
  27. /// <summary>
  28. /// Asynchronously wait for the process to exit.
  29. /// </summary>
  30. /// <param name="process">The process to wait for.</param>
  31. /// <param name="cancelToken">A <see cref="CancellationToken"/> to observe while waiting for the process to exit.</param>
  32. /// <returns>True if the task exited normally, false if cancelled before the process exited.</returns>
  33. public static async Task<bool> WaitForExitAsync(this Process process, CancellationToken cancelToken)
  34. {
  35. if (!process.EnableRaisingEvents)
  36. {
  37. throw new InvalidOperationException("EnableRisingEvents must be enabled to async wait for a task to exit.");
  38. }
  39. // Add an event handler for the process exit event
  40. var tcs = new TaskCompletionSource<bool>();
  41. process.Exited += (sender, args) => tcs.TrySetResult(true);
  42. // Return immediately if the process has already exited
  43. if (process.HasExitedSafe())
  44. {
  45. return true;
  46. }
  47. // Register with the cancellation token then await
  48. using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe())))
  49. {
  50. return await tcs.Task.ConfigureAwait(false);
  51. }
  52. }
  53. /// <summary>
  54. /// Gets a value indicating whether the associated process has been terminated using
  55. /// <see cref="Process.HasExited"/>. This is safe to call even if there is no operating system process
  56. /// associated with the <see cref="Process"/>.
  57. /// </summary>
  58. /// <param name="process">The process to check the exit status for.</param>
  59. /// <returns>
  60. /// True if the operating system process referenced by the <see cref="Process"/> component has
  61. /// terminated, or if there is no associated operating system process; otherwise, false.
  62. /// </returns>
  63. private static bool HasExitedSafe(this Process process)
  64. {
  65. try
  66. {
  67. return process.HasExited;
  68. }
  69. catch (InvalidOperationException)
  70. {
  71. return true;
  72. }
  73. }
  74. }
  75. }