HarmonyOS 鸿蒙Next 无法取消 taskpool.SequenceRunner
taskpool.SequenceRunner 的任务无法用 taskpool.cancel 取消,而 SequenceRunner 本身也没有提供取消的方法
操作步骤: 1、初始化 taskpool.SequenceRunner 对象 2、向 taskpool.SequenceRunner 对象提交任务 3、使用 taskpool.cancel 取消还未开始执行的任务 4、闪退,提示 The task to cancel does not exist, taskpool:: task is not executed or has been executed
测试代码:
test() {
console.log(`item test9`)
let task1:taskpool.Task = new taskpool.Task(ThreadPool.additionDelay, 5000);
let task2:taskpool.Task = new taskpool.Task(ThreadPool.additionDelay, 2000);
let task3:taskpool.Task = new taskpool.Task(ThreadPool.additionDelay, 4000);
let runner:taskpool.SequenceRunner = new taskpool.SequenceRunner();
task1.onStartExecution(() => {
console.log(`task1 start`)
})
task2.onStartExecution(() => {
console.log(`task2 start`)
})
task3.onStartExecution(() => {
console.log(`task3 start`)
})
runner.execute(task1).then(() => {
console.log(`task1 done`)
})
runner.execute(task2).then(() => {
console.log(`task2 done`)
})
runner.execute(task3).then(() => {
console.log(`task3 done`)
})
setTimeout(() => {
console.log(`task2 cancel`)
taskpool.cancel(task2)
}, 2000)
console.log(`test9 done`)
}
[@Concurrent](/user/Concurrent)
export async function additionDelay(delay:number) {
let start: number = new Date().getTime();
while (new Date().getTime() - start < delay) {
continue;
}
}
更多关于HarmonyOS 鸿蒙Next 无法取消 taskpool.SequenceRunner的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
未执行的任务使用taskpool.cancel可以取消,https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-taskpool-V13#taskpoolcancel10。 已经执行的任务无法取消。
以下面例子为例,点击click开始执行。task3在6s时执行,6s前点击cancel,可以cancel成功。6s后点击cancel按钮会报错[(:662)(CancelSeqRunnerTask)] taskpool:: sequenceRunner task has been executed
import { JSON, taskpool } from '@kit.ArkTS';
@Concurrent
function additionDelay(delay: number): void {
let start: number = new Date().getTime();
while (new Date().getTime() - start < delay) {
continue;
}
}
@Concurrent
function waitForRunner(finalString: string): string {
return finalString;
}
@Entry
@Component
struct ConcurrentCancelDemo {
private task1: taskpool.Task | undefined;
private task2: taskpool.Task | undefined;
private task3: taskpool.Task | undefined;
async seqRunner() {
console.log('seqrunner: xxx')
let finalString: string = "";
this.task1 = new taskpool.Task(additionDelay, 3000);
this.task2 = new taskpool.Task(additionDelay, 2000);
this.task3 = new taskpool.Task(additionDelay, 1000);
let task4: taskpool.Task = new taskpool.Task(waitForRunner, finalString);
let runner: taskpool.SequenceRunner = new taskpool.SequenceRunner();
runner.execute(this.task1).then(() => {
finalString += 'a';
console.info("seqrunner: task1 done.");
});
runner.execute(this.task2).then(() => {
finalString += 'b';
console.info("seqrunner: task2 done");
});
runner.execute(this.task3).then(() => {
finalString += 'c';
console.info("seqrunner: task3 done");
});
await runner.execute(task4);
console.info("seqrunner: task4 done, finalString is " + finalString);
}
build() {
Row() {
Button('click').onClick(async () => {
await this.seqRunner();
});
Button('cancel').onClick(async () => {
try {
taskpool.cancel(this.task3);
} catch (e) {
console.error("seqrunner: " + JSON.stringify(e));
}
});
}
}
}
更多关于HarmonyOS 鸿蒙Next 无法取消 taskpool.SequenceRunner的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙系统中,针对taskpool.SequenceRunner
无法取消的问题,这通常涉及到任务调度和线程管理的内部机制。SequenceRunner
作为任务池中的序列运行器,一旦启动,其生命周期管理较为严格,直接取消可能涉及状态同步和资源释放的复杂逻辑。
在鸿蒙的任务调度模型中,任务一旦进入运行状态,通常只能通过任务自身的逻辑或特定信号(如中断、超时等)来优雅地结束。若需停止SequenceRunner
,可考虑以下几种方式(非直接取消,而是间接控制):
-
设置标志位:在任务执行逻辑中定期检查一个全局或线程局部的标志位,当需要停止时,修改此标志位,任务检测到后自行退出。
-
任务超时机制:为任务设置合理的超时时间,超时后任务自动结束。
-
依赖外部信号:利用鸿蒙系统的信号机制,当需要停止任务时,发送特定信号给任务所在线程,任务内部捕获并处理该信号。
这些方法依赖于任务自身的设计和对系统API的合理利用。若任务逻辑复杂或涉及资源锁定,还需考虑资源释放和状态一致性等问题。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html