简体   繁体   English

如何在 Kotlin 中杀死协程?

[英]How to kill a coroutine in Kotlin?

I,m want to create a metronome app with Kotlin for Android.我想用 Kotlin for Android 创建一个节拍器应用程序。 I,m starting coroutine for click playback:我正在启动协程以进行点击播放:

playBtn.setOnClickListener {
        if (!isPlaying) {
            playBtn.setText(R.string.stop)
            isPlaying = true
        }
        else {
            isPlaying = false
            playBtn.setText(R.string.play)
        }

        if (isPlaying) {
            GlobalScope.launch {
                while (isPlaying) {
                    delay(bpmToMillis)
                    launch { metro1.start() }
                }
            }
        }
    }

It works fine, but if you rapidly taping "play" button, it starts another thread with metronome sound.它工作正常,但如果你快速点击“播放”按钮,它会启动另一个带有节拍器声音的线程。 So, instead of one "click" it plays two or three with little delay.因此,它不是“单击”一次,而是几乎没有延迟地播放两三个。

I tried to move code blocks and using sleep and TimeUnit, but it doesnt work.我尝试移动代码块并使用 sleep 和 TimeUnit,但它不起作用。

I,m noob, no hate pls:)我是菜鸟,不讨厌请:)

playBtn.setOnClickListener {
    if (!isPlaying) {
        playBtn.setText(R.string.stop)
        isPlaying = true
    }
    else {
        isPlaying = false
        playBtn.setText(R.string.play)
    }

    if (isPlaying) {
// assigned coroutine scope to variable 
        var job = GlobalScope.launch {
            while (isPlaying) {


if(isActive){
                delay(bpmToMillis)
                launch { metro1.start() }
}
            }
//some condition to cancel the coroutine
job.cancel()
        }
    }
}

Thanks for help, everyone!谢谢大家的帮助! You pushed me in right direction.你把我推向了正确的方向。

Only way playback is not bugged if I adding new var with null or empty coroutine:如果我添加带有 null 或空协程的新 var,播放不会被窃听的唯一方式:

var job: Job? = null
// or
var job: Job = GlobalScope.launch {}

and then assign value within the if-statement:然后在 if 语句中赋值:

playBtn.setOnClickListener {
        if (!isPlaying) {
            playBtn.setText(R.string.stop)
            isPlaying = true
        }
        else {
            isPlaying = false
            playBtn.setText(R.string.play)
            //job cancel here
            job?.cancel()
        }
        if (isPlaying) {
            //var assigment here
            job = GlobalScope.launch {
                while (isPlaying) {
                    metro1.start()
                    delay(bpmToMillis)
                }
            }
        }
    }

Otherwise job is not visible frome upper levels and you cant REALLY cancel it when you need to, i think:)否则工作在上层是不可见的,你不能在需要的时候真正取消它,我认为:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM