gpt4 book ai didi

c# - process.WaitForExit(int32) 异步

转载 作者:行者123 更新时间:2023-11-30 16:03:18 28 4
gpt4 key购买 nike

我正在使用以下提供的解决方案: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 ) 我应该在某个时候处理 cancellationToken,那么我不应该在方法中使用 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;
}

最佳答案

您需要更改方法签名并应用流程本身的结果。例如考虑以下内容:

/// <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,
int milliseconds,
CancellationToken cancellationToken = default(CancellationToken))
{
if (process.HasExited)
{
return Task.CompletedTask;
}

var tcs = new TaskCompletionSource<object>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => tcs.TrySetResult(null);
if (cancellationToken != default(CancellationToken))
{
cancellationToken.Register(tcs.SetCanceled);
}

return process.HasExited
? Task.CompletedTask
: Task.WhenAny(tcs.Task, Task.Delay(milliseconds));
}

现在,如果进程还没有结束,任务仍然会在延迟后返回。另一种方法是执行 Task.Run 并调用所需的重载,如下所示:

/// <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,
int milliseconds,
CancellationToken cancellationToken = default(CancellationToken)) =>
process.HasExited
? Task.CompletedTask
: Task.Run(() => process.WaitForExit(milliseconds), cancellationToken);
}

关于c# - process.WaitForExit(int32) 异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36545858/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com