Skip to content

Commit

Permalink
[Fix] Cancel previous polling job upon restarting HWiNFO (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil0v3s authored Nov 17, 2024
1 parent 9b361e8 commit 03600fa
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package br.com.firstsoft.core.os.hwinfo
import br.com.firstsoft.core.os.resource.NativeResourceLoader
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
Expand All @@ -14,6 +16,7 @@ object HwInfoProcessManager {
private var process: Process? = null
private const val MAX_RETRIES = 1
private var currentRetries = 0
private var pollingJob: Job? = null

init {
currentRetries = 0
Expand All @@ -29,7 +32,8 @@ object HwInfoProcessManager {
command("cmd.exe", "/c", file)
}.start()

observeHwInfoPollingTime()
pollingJob?.cancel()
pollingJob = observeHwInfoPollingTime()
}

fun stop() {
Expand All @@ -40,6 +44,11 @@ object HwInfoProcessManager {
process = null
}

private fun restart() {
stop()
start()
}

private fun overwriteSettings() {
try {
val sourceSettings = NativeResourceLoader.load("/hwinfo/HWiNFO64.INI.src")
Expand All @@ -55,21 +64,21 @@ object HwInfoProcessManager {

HwInfoReader
.currentData
.cancellable()
.map { it.header }
.collectLatest {
accumulator += 500
if (accumulator < TimeUnit.SECONDS.toMillis(5)) return@collectLatest

if (accumulator >= TimeUnit.SECONDS.toMillis(5)) {
accumulator = 0

if (lastPollTime != it.pollTime) {
accumulator = 0
when {
lastPollTime != it.pollTime -> {
lastPollTime = it.pollTime
currentRetries = 0
} else if (currentRetries < MAX_RETRIES) {
}
lastPollTime == it.pollTime && currentRetries < MAX_RETRIES -> {
currentRetries++
stop()
start()
return@collectLatest
restart()
}
}
}
Expand Down

0 comments on commit 03600fa

Please sign in to comment.