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
nabrezzeltOPโ€ข3mo ago
No its provided by Google OR-Tools for solving constraint programming models
nabrezzelt
nabrezzeltOPโ€ข3mo ago
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
nabrezzeltOPโ€ข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
nabrezzeltOPโ€ข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
nabrezzeltOPโ€ข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
nabrezzeltOPโ€ข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
nabrezzeltOPโ€ข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
nabrezzeltOPโ€ข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
nabrezzeltOPโ€ข3mo ago
no im using Google.OrTools.Sat.CpSolver
Sossenbinder
Sossenbinderโ€ข3mo ago
Ah, I see. Not familiar with the lib ๐Ÿ˜„
nabrezzelt
nabrezzeltOPโ€ข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