【发布时间】:2016-08-01 10:44:44
【问题描述】:
我正在使用https://stackoverflow.com/a/19104345/2713516 中提供的解决方案来运行 WaitForExit 异步,但是,我想使用 Int32 参数重载 (https://msdn.microsoft.com/en-us/library/ty0d8k56(v=vs.110).aspx),如 process.WaitForExit(10000)。
如何更改此扩展方法以使其与超时参数一起使用?
附带问题:我还看到有人提到 (https://stackoverflow.com/a/32994778/2713516) 我应该在某个时候处理 cancelToken,那么我不应该在方法中使用 dispose/using 吗?以及如何?
/// <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;
}
【问题讨论】:
-
您可以使用stackoverflow.com/questions/25683980/…中的“无内置超时”模式
-
你已经传递了
CancellationToken- 这已经比超时更有用了。超时后取消,例如new CancellationTokenSource(timeout).Token. -
感谢您的建议
标签: c# .net async-await extension-methods