ProcessExtensions.cs 1.1 KB

12345678910111213141516171819202122232425262728
  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>A task that will complete when the process has exited, cancellation has been requested, or an error occurs.</returns>
  18. /// <exception cref="OperationCanceledException">The timeout ended.</exception>
  19. public static async Task WaitForExitAsync(this Process process, TimeSpan timeout)
  20. {
  21. using (var cancelTokenSource = new CancellationTokenSource(timeout))
  22. {
  23. await process.WaitForExitAsync(cancelTokenSource.Token).ConfigureAwait(false);
  24. }
  25. }
  26. }
  27. }