From c96eb3ccf1b7c98110d392d09b1b0cef35c89e6a Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 16 Apr 2021 13:03:06 +0800 Subject: [PATCH] Fix TimedTask if not changed --- .../src/util/CoroutineScopeUtils.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/mirai-console/src/util/CoroutineScopeUtils.kt b/backend/mirai-console/src/util/CoroutineScopeUtils.kt index 490bbfe73..99e760d6c 100644 --- a/backend/mirai-console/src/util/CoroutineScopeUtils.kt +++ b/backend/mirai-console/src/util/CoroutineScopeUtils.kt @@ -68,13 +68,19 @@ internal class TimedTask( // `delay` always checks for cancellation lastChangedTime.loop { last -> val current = currentTimeMillis() - if (current - last > intervalMillis) { - if (!lastChangedTime.compareAndSet(last, UNCHANGED)) return@loop - action() + if (last == UNCHANGED) { + runIgnoreException { + delay(3.seconds) // accuracy not necessary + } ?: return@launch + } else { + if (current - last > intervalMillis) { + if (!lastChangedTime.compareAndSet(last, UNCHANGED)) return@loop + action() + } + runIgnoreException { + delay(3.seconds) // accuracy not necessary + } ?: return@launch } - runIgnoreException { - delay(3.seconds) // accuracy not necessary - } ?: return@launch } } }