samuel
06/16/2020, 5:09 PMwithTimeout(20000) {
try{
while (true) {
// read input from socket and exit while block when exception is thrown
// or expected data has been received
}
}catch(e: Exception) {
// Handle the exception here
}
}
I also tried to use this, but the code within`scope.launch` was never executed but the timeout worked as expected
val scope: CoroutineScope = CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO>)
withTimeout(20000){
suspendCancellableCoroutine<String>{ continuation ->
continuation.invokeOnCancellation {
// Handle my exception here
}
scope.launch {
while (true) {
// read input from socket and exit while block when exception is thrown
// or expected data has been received
}
}
}
}
Could someone please help give me some pointers in what i might be missing or even perhaps a better way to approach this?Zach Klippenstein (he/him) [MOD]
06/16/2020, 5:16 PMwhile(isActive)
and wrap your IO in runInterruptible
(https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-interruptible.htmlsamuel
06/16/2020, 5:25 PMrunInterruptible
doesn’t seem to exist for me 🤔Zach Klippenstein (he/him) [MOD]
06/16/2020, 5:26 PMsamuel
06/16/2020, 5:27 PM1.3.7
currentlyZach Klippenstein (he/him) [MOD]
06/16/2020, 5:28 PMsamuel
06/16/2020, 5:30 PMZach Klippenstein (he/him) [MOD]
06/16/2020, 5:55 PMisActive
check. It's slightly more wasteful but still better than not cancelling at allsamuel
06/16/2020, 6:13 PMrunInterruptible
working but it did not actually cancel the block 🤔