Im using Monix for asynchronous task workflow.
How do we kill a running Task
?
Task{ println("sleep")
Thread.sleep(200)
println("effect") }
.doOnCancel(Task(println("canceled")))
.timeout(100.milli) // timeout will do cancel
.runOnComplete(println)
@> Failure(java.util.concurrent.TimeoutException: Task timed-out after 100 milliseconds of inactivity)
sleep
canceled
effect <--- what !? , task is running. Isn't it canceled !?
My current solution is ugly in my opinion(the flag checking hinders the code reusing):
var flag=true
Task{
println("sleep")
Thread.sleep(200)
if (flag)
println("effect")
}
.doOnCancel(Task{ flag=false; println("canceled") })
.timeout(100.milli) // timeout will do cancel
If it is impossible, how do we kill a scheduled while not-yet-ran Task
?
My failed attempt is :
Task{ println("sleep"); Thread.sleep(200) }
.map{ _ => println("effect") }
.doOnCancel(Task(println("canceled")))
.timeout(100.milli) // timeout will do cancel
.runOnComplete(println)
Sadly it still shows the effect after the cancel happened. I hope that the scheduled and not-yet-ran Task can be canceled (the .map(...)
is another Task
, right?)