Landry Norris
01/17/2023, 6:22 PMisActive
return true if I launch
, but the dispatcher hasn’t picked up the job yet? I launch the jobs on a singleThreadContext and there’s no delay or suspension in the job, so I’d assume only one runs at a time. When launching a new job, I want to cancel the last one if it hasn’t started yet, so I have if(latestSeek?.isActive != true) latestSeek?.cancel()
, but the jobs seem to pile up anyways. Each job overwrites the result of the previous job, so any tasks between the last running and the next job is wasted time.kevin.cianfarini
01/17/2023, 6:47 PMCoroutineStart.UNDISPATCHED
here but idk if thats what you need.Landry Norris
01/17/2023, 6:48 PMkevin.cianfarini
01/17/2023, 6:50 PMLandry Norris
01/17/2023, 7:00 PMkevin.cianfarini
01/17/2023, 7:01 PMLandry Norris
01/17/2023, 7:01 PMkevin.cianfarini
01/17/2023, 7:02 PMlaunch(start = UNDISPATCHED) {
withContext(singleThread) { cFunction() }
}
Landry Norris
01/17/2023, 7:04 PMkevin.cianfarini
01/17/2023, 7:04 PMLandry Norris
01/17/2023, 7:05 PMkevin.cianfarini
01/17/2023, 7:05 PM* By default, the coroutine is immediately scheduled for execution.
* Other start options can be specified via `start` parameter. See [CoroutineStart] for details.
* An optional [start] parameter can be set to [CoroutineStart.LAZY] to start coroutine _lazily_. In this case,
* the coroutine [Job] is created in _new_ state. It can be explicitly started with [start][Job.start] function
* and will be started implicitly on the first invocation of [join][Job.join].
Are you calling start
?Landry Norris
01/17/2023, 7:06 PMfranztesca
01/17/2023, 7:16 PMval job =launch(singleThreadDispatcher) { withContext(NonCancellable) { expensiveTask() } }
And then
job.cancelAndJoin
will return immediately if the task is not started, or wait until the completion of the expensive task otherwise.Landry Norris
01/17/2023, 7:17 PMcancelAndJoin
block the thread I call launch on?franztesca
01/17/2023, 7:20 PMval tasksToDo = MutableStateFlow<suspend () -> Unit>
myScope.launch(singleThreadDispatcher) {
taskToDo.collect {
it.invoke()
}
}
Landry Norris
01/17/2023, 7:21 PMkevin.cianfarini
01/17/2023, 7:26 PMcancalAndJoin
will block until the underlying blocking C call returns. Note that cancellation will have no impact on the C call unless you explicitly check for it somehow.Nick Allen
01/19/2023, 5:47 AMlaunch
multiple times is for starting concurrent coroutines. That doesn't match your use case. Keep in mind launched code does not always run in the same order it was launched.
If you want things to run one after another, you want a single coroutine as is described a few comments up though definitely use a conflated channel. channel = queue, MutableStateFlow = observable value. MutableStateFlow will reference the task even after it's finished running.