Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin Coroutine is not cancelled #2

Open
JkMulti opened this issue Mar 1, 2021 · 3 comments
Open

Kotlin Coroutine is not cancelled #2

JkMulti opened this issue Mar 1, 2021 · 3 comments

Comments

@JkMulti
Copy link

JkMulti commented Mar 1, 2021

Hi, first of all a very big thanks for async task alternative. The code has one exception, it is not cancelling the coroutine/job when cancel is called.
fun cancel(mayInterruptIfRunning: Boolean) {
if (preJob == null || bgJob == null) {
printLog("$taskName has already been cancelled/finished/not yet started.")
return
}
if (mayInterruptIfRunning || (!preJob!!.isActive && !bgJob!!.isActive)) {
isCancelled = true
status = Status.FINISHED
if (bgJob!!.isCompleted) {
GlobalScope.launch(Dispatchers.Main) {
onCancelled(bgJob!!.await())
}
}
preJob?.cancel(CancellationException("PreExecute: Coroutine Task cancelled"))
bgJob?.cancel(CancellationException("doInBackground: Coroutine Task cancelled"))

        if (bgJob!!.isCancelled) {
            printLog("Bg JOB has been cancelled.")
        }
        printLog("$taskName has been cancelled.")
    }
}
@prajwalgambhir
Copy link
Collaborator

Please provide the code?

@dineshrajamanikam
Copy link

dineshrajamanikam commented Aug 23, 2023

Any solution for the mentioned doubt?

@JkMulti
Copy link
Author

JkMulti commented Sep 1, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants