C
C#β€’3mo 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β€’3mo ago
What does the .Solve() method do? You can pass the cancellationtoken inside and check for cancellation accordingly
nabrezzelt
nabrezzeltβ€’3mo ago
No its provided by Google OR-Tools for solving constraint programming models
nabrezzelt
nabrezzeltβ€’3mo ago
only thing that can cancel the solve operation seems to be in a Callback https://developers.google.com/optimization/cp/cp_tasks#solution-limit
Sossenbinder
Sossenbinderβ€’3mo 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β€’3mo ago
is there no option the "kill" a task?
Sossenbinder
Sossenbinderβ€’3mo ago
Not necessarily, cancellation is usually a cooperative thing, there's no real way to do preemptive cancellation of a task.
nabrezzelt
nabrezzeltβ€’3mo ago
hm okay then i think I have to go the way with the solution callback - hope this works πŸ˜„
Sossenbinder
Sossenbinderβ€’3mo 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β€’3mo 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β€’3mo 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β€’3mo 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β€’3mo 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β€’3mo 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β€’3mo 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β€’3mo ago
bad thing is that i'm using CpSolver :/ issue is for MipSolver
Sossenbinder
Sossenbinderβ€’3mo ago
No description
Sossenbinder
Sossenbinderβ€’3mo ago
So this is not what you use?
nabrezzelt
nabrezzeltβ€’3mo ago
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...
nabrezzelt
nabrezzeltβ€’3mo ago
no im using Google.OrTools.Sat.CpSolver
Sossenbinder
Sossenbinderβ€’3mo ago
Ah, I see. Not familiar with the lib πŸ˜„
nabrezzelt
nabrezzeltβ€’3mo ago
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 πŸ˜…
Want results from more Discord servers?
Add your server