Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to pass in a cancellation token #18

Closed
rollemira opened this issue Jun 28, 2017 · 3 comments
Closed

A way to pass in a cancellation token #18

rollemira opened this issue Jun 28, 2017 · 3 comments
Milestone

Comments

@rollemira
Copy link

I'm finding myself needing to give the constructed task created a cancellation token. If there is a way to do that, I'm just not finding it.

@rollemira
Copy link
Author

Never mind, I'm sorry. Just my ignorance of tasks. Turns out when I return it as a task and call for .Wait() in the managing class I can pass the CancellationToken in there.

@madelson
Copy link
Owner

@rollemira be careful; Task.Wait(CancellationToken) will only cancel the wait, not the actual Command and underlying process.

If you want to actually abort the command via cancellation token, your best bet today is:

var command = Command.Run(...);
// when the token is canceled, abort the command
cancellationToken.Register(() => command.Kill());
// could pass the token in here, but that wouldn't add much since killing the process will also end the wait
// at that point, you'll either see an exception (if you set ThrowOnError(), or a successful return with an error exit code (the default)
command.Task.Wait();

That said, it would be nice if you could pass a CancellationToken in via options:

var command = Command.Run(
    "somecommand", 
     new[] { "some", "arguments" }, 
     options: o => o.CancellationToken(cancellationToken)
);

If we added this API, then a cancel would not only abort the process but would also cause Command.Task to change to the Canceled status.

@rollemira
Copy link
Author

Sweet! I'll use your recommended solution for now. Thanks!

@madelson madelson added this to the 1.4 milestone Jun 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants