From d7a8ca381a3b67f34fd9f2b1cf06084aff9dd39c Mon Sep 17 00:00:00 2001 From: sfchu Date: Wed, 25 Jun 2025 16:58:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EFAQ=EF=BC=9A=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E5=9C=A8TaskPool=E5=92=8CWoker=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87Context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/pages/TaskPoolGetContext.ets | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ArkTS/entry/src/main/ets/pages/TaskPoolGetContext.ets diff --git a/ArkTS/entry/src/main/ets/pages/TaskPoolGetContext.ets b/ArkTS/entry/src/main/ets/pages/TaskPoolGetContext.ets new file mode 100644 index 0000000..4e8ba52 --- /dev/null +++ b/ArkTS/entry/src/main/ets/pages/TaskPoolGetContext.ets @@ -0,0 +1,58 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何在TaskPool和Woker获取上下文Context + */ + +// [Start task_pool_get_context] +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { taskpool } from '@kit.ArkTS'; + +// Support for ordinary functions and passing of reference parameters as input. +@Concurrent +function printArgs(args: string): string { + console.info('func: ' + args); + return args; +} + +@Entry +@Component +struct TaskPoolGetContext { + @State message: string = 'Hello World'; + // Obtain the context. + uiContext = this.getUIContext(); + + async taskpoolExecute(): Promise { + let task: taskpool.Task = new taskpool.Task(printArgs, 'create task, then execute'); + hilog.info(0x0000, 'TaskPoolGetContext', 'taskpool.execute(task) result: ' + await taskpool.execute(task)); + hilog.info(0x0000, 'TaskPoolGetContext', + 'taskpool.execute(function) result: ' + await taskpool.execute(printArgs, 'execute task by func')); + } + + build() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.taskpoolExecute(); + }) + } + .width('100%') + .height('100%') + } +} +// [End task_pool_get_context] \ No newline at end of file -- Gitee