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:
21 Replies
What does the .Solve() method do? You can pass the cancellationtoken inside and check for cancellation accordingly
No its provided by Google OR-Tools for solving constraint programming models
only thing that can cancel the solve operation seems to be in a Callback https://developers.google.com/optimization/cp/cp_tasks#solution-limit
Google for Developers
Setting solver limits Β |Β OR-Tools Β |Β Google for Developers
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
is there no option the "kill" a task?
Not necessarily, cancellation is usually a cooperative thing, there's no real way to do preemptive cancellation of a task.
hm okay then i think I have to go the way with the solution callback - hope this works π
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
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 π
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
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 π π€ ...
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
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 ...
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?
bad thing is that i'm using CpSolver :/ issue is for MipSolver
So this is not what you use?
https://github.com/google/or-tools/issues/2310 the seem not to be interested in early cancellation.... π thats exacly the way im now doing it
GitHub
Support stopping the search from outside the solution callback Β· Is...
What language and solver does this apply to? I'm using CP-SAT with Java, but this feature would be useful for everything Describe the problem you are trying to solve. I'm running a long sea...
no im using
Google.OrTools.Sat.CpSolver
Ah, I see. Not familiar with the lib π
hm okay thanks anyway - i will test this and if it doesnt work it will be a console app where ctrl+c works π or a using a thread π