ProcessExtensions.cs 3.3 KB

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