whats wrong with this code? why isActive not detec...
# announcements
c
whats wrong with this code? why isActive not detecting the cancel https://pastebin.com/UcYmAAJj
d
Your
launch
coroutine never suspends, so the
cancelAndJoin
is only ever reached when the
launch
coroutine is complete. This is because you are inside a
runBlocking
context, which means there is only one thread executing all coroutines in an event loop.
If you use
delay(100)
instead of
Thread.sleep
you should see the expected result.
Actually, I am wrong. You are not detecting
isActive == false
because cancellation is handled transparently by
delay
(or even
Thread.sleep
through interrupts). If you want to detect cancellation you'd have to use
try-finally
c
any example for that? to handle cancellation
d
Just surrounding your code with try-catch should make you receive a
CancellationException
z
FYI there's also a dedicated #coroutines channel for future questions like this