I am using the solution provided in: https://stackoverflow.com/a/19104345/2713516 to run WaitForExit async, however, I want to use the Int32 parameter overload (https://msdn.microsoft.com/en-us/library/ty0d8k56(v=vs.110).aspx), as in process.WaitForExit(10000).
How do I alter this extension method so it works with the timeout parameter?
Side question: I also saw someone mentioned (https://stackoverflow.com/a/32994778/2713516) that I should dispose the cancellationToken at some point, shouldn't I use a dispose/using within the method then? and How?
/// <summary>
/// Waits asynchronously for the process to exit.
/// </summary>
/// <param name="process">The process to wait for cancellation.</param>
/// <param name="cancellationToken">A cancellation token. If invoked, the task will return
/// immediately as canceled.</param>
/// <returns>A Task representing waiting for the process to end.</returns>
public static Task WaitForExitAsync(this Process process,
CancellationToken cancellationToken = default(CancellationToken))
{
var tcs = new TaskCompletionSource<object>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => tcs.TrySetResult(null);
if(cancellationToken != default(CancellationToken))
cancellationToken.Register(tcs.SetCanceled);
return tcs.Task;
}