You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@Elron1179 thanks for reporting. I looked into this and was able to reproduce.
The problem is that the command you are running launches cmd.exe which then launches PING.exe as a subprocess. When the Timeout goes off, it kills the cmd process, but this doesn't cause the PING process to exit. Because the PING process hasn't exited and because it inherited the console stream of cmd, StandardOutput stays open. Therefore the command won't exit because the redirection operation hasn't finished.
Luckily, there are (at least) two clean solutions that I can see. The first is to avoid the cmd indirection and call PING directly:
await Command.Run(
executable: "PING.exe",
arguments: new object[] { "127.0.0.1", "/t" },
options: o => o.Timeout(TimeSpan.FromSeconds(1))
)
.RedirectTo(new FileInfo(@"C:\Temp\shellTestOuput.txt"))
.Task;
The second is to do a graceful shutdown via signals in order to kill all processes on the current console.
var command = Command.Run(
executable: "cmd.exe",
arguments: new[] { "/c", "ping", "127.0.0.1", "/t" },
options: o => { } // no timeout
)
.RedirectTo(new FileInfo(@"C:\Temp\shellTestOuput.txt"));
// manually set up a timeout with graceful shutdown
Task.Delay(TimeSpan.FromSeconds(1))
.ContinueWith(_ =>
{
if (!command.TrySendSignalAsync(CommandSignal.ControlC).Result
|| !command.Task.Wait(TimeSpan.FromSeconds(1))
{
// only resort to kill if sending the signal fails (it shouldn't) or if the command doesn't
// promptly exit after the signal is sent
command.Kill();
}
}
It would be nice if the library made it easier to figure this out/do this correctly without having to ask here. I've linked some enhancement ideas which I think could help in the future:
I am having trouble using the Timeout option in combination with the RedirectTo-Method.
Example-code:
When I comment-out the RedirectTo line, the timeout is respected, but if I put it back in, the timeout is ignored.
Is this a bug or by design?
(If this a question better suited for stack-overflow, please say so and I'll move it there.)
Thank you.
Edit:
Environment:
The text was updated successfully, but these errors were encountered: