- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Kotlin 中有一个简单的应用程序,它绘制一个矩形,然后使用 fixedRateTimer 每秒更新矩形的位置 30 次。我遇到的问题是,当我关闭显示矩形的窗口时,应用程序继续运行,我必须按下 Intellij 内的红色方块才能真正停止它。
我尝试在关闭窗口之前取消 fixedRateTimer 但应用程序仍在运行它似乎没有做任何事情。如果我在没有 fixedRateTimer 的情况下运行应用程序,它只会显示方块,然后当我关闭窗口时它会停止应用程序。
import javafx.scene.paint.Color
import tornadofx.*
import kotlin.concurrent.fixedRateTimer
class MyApp: App(MyView::class)
class MyView : View() {
override val root = stackpane {
group {
rectangle {
fill = Color.LIGHTGRAY
width = 600.0
height = 480.0
}
val myRect = rectangle {
fill = Color.BLUEVIOLET
width = 30.0
height = 30.0
x = 100.0
y = 100.0
}
fixedRateTimer("default", false, 0L, 1000/30) {
myRect.x += 1
if(myRect.x > 200) this.cancel()
}
}
}
}
最佳答案
您正在取消 TimerTask,而不是计时器。要么通过 daemon = true
创建一个守护线程,或者确保你保存了 Timer
从 fixedRateTimer()
返回的实例调用,并在某个时候调用 cancel
在它上面停止非守护进程线程在你退出之前运行计时器。
JVM 会在有守护线程运行时退出,但在有非守护线程时不会退出。
关于kotlin - 使用 fixedRateTimer 时 Kotin 应用程序不会停止(即使窗口关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58080453/
我得到一个错误: Optional long parameter 'count' is present but cannot be translated into a null value due t
不知何故,我无法通过 mockito 方法来模拟并获取空指针。不知道为什么它失败了! import io.github.xxxx.repository.ProductRepository import
我正在开发一个简单的联系人应用程序,用于存储联系人的姓名、电子邮件和号码。 语言: Kotlin 架构:MVVM 但我收到一个错误:lateinit property addContactViewMo
我在 Kotlin 中有一个简单的应用程序,它绘制一个矩形,然后使用 fixedRateTimer 每秒更新矩形的位置 30 次。我遇到的问题是,当我关闭显示矩形的窗口时,应用程序继续运行,我必须按下
我是一名优秀的程序员,十分优秀!