C
C#โ€ข3w ago
nabrezzelt

Cancel long running task/method

Hi, i have a question: I have a WPF app and want to call the method solver.Solve(); that could take some time but i have not "control" over the method. How can i put that onto another thread that is also cancelable? I tried this but it seems that the cancellationToken is only checked on entering this Task.Run:
await Task.Run(() =>
{
return solver.Solve();
}, cancellationToken);
await Task.Run(() =>
{
return solver.Solve();
}, cancellationToken);
21 Replies
Sossenbinder
Sossenbinderโ€ข3w ago
What does the .Solve() method do? You can pass the cancellationtoken inside and check for cancellation accordingly
nabrezzelt
nabrezzeltโ€ข3w ago
No its provided by Google OR-Tools for solving constraint programming models
nabrezzelt
nabrezzeltโ€ข3w ago
Sossenbinder
Sossenbinderโ€ข3w ago
Passing a cancellationToken to Task.Run does not imply that everything within the Task.Run is suddenly under a cancellation umbrella, so you'd probably have to find a way to make the functionality respect the token as well
nabrezzelt
nabrezzeltโ€ข3w ago
is there no option the "kill" a task?
Sossenbinder
Sossenbinderโ€ข3w ago
Not necessarily, cancellation is usually a cooperative thing, there's no real way to do preemptive cancellation of a task.
nabrezzelt
nabrezzeltโ€ข3w ago
hm okay then i think I have to go the way with the solution callback - hope this works ๐Ÿ˜„
Sossenbinder
Sossenbinderโ€ข3w ago
Ideally the library maintainers would add support for cancellation, if that's a reasonable thing to do. The other options would probably be having a subprocess run with the code, which can be cancelled on demand, or your could try running it in a dedicated Thread you could then interrupt. But that would required a truly dedicated one, since you put the task on the ThreadPool with Task.Run and you don't want to mess with those ideally
nabrezzelt
nabrezzeltโ€ข3w ago
okay it that works but the cancellation is very delayed ๐Ÿค” is there a difference between a thread and subprocess? because i dont think they will add a other way of cancellation any time soon ๐Ÿ˜„
Cortex
Cortexโ€ข3w ago
I don't think there's a way around it, the cancellation token check is probably in a place where it's safe to do so
nabrezzelt
nabrezzeltโ€ข3w ago
my solution is now to add a SolutionCallback and every time a new solution is found i'll check if the CT is cancelled and the stop the search ๐Ÿ˜“ ๐Ÿค” ...
Sossenbinder
Sossenbinderโ€ข3w ago
A Thread would be lightweight and you don't have to serialize input / output across processes, but it might negatively affect your app since both share program memory. A process would be fully isolated, but might not be as lightweight and harder to get input / output across
Sossenbinder
Sossenbinderโ€ข3w ago
GitHub
Expose InterruptSolve to .Net wrapper ยท Issue #423 ยท google/or-tools
To support user cancellation of long running solves, it would be good to expose the MpSolver.InterruptSolve() method. Even better would be adding support for the standard .net CancellationToken in ...
Sossenbinder
Sossenbinderโ€ข3w ago
Maybe this is a hint https://github.dev/google/or-tools So there seems to be a way to cancel it from the outside, if that works for you?
nabrezzelt
nabrezzeltโ€ข3w ago
bad thing is that i'm using CpSolver :/ issue is for MipSolver
Want results from more Discord servers?
Add your server