1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// [Start worker_receive_child_thread_message] 17import { worker } from '@kit.ArkTS'; 18import resource from '../util/resource'; 19 20const workerInstance: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 21 22@Entry 23@Component 24struct Index { 25 @State message: string = 'Listener task'; 26 27 build() { 28 Column() { 29 Text(this.message) 30 .id('HelloWorld') 31 .fontSize(50) 32 .fontWeight(FontWeight.Bold) 33 .onClick(() => { 34 workerInstance.postMessage({ type: 'End' }); 35 workerInstance.onmessage = (event) => { 36 console.info(resource.resourceToString($r('app.string.Information')), event.data); 37 } 38 // 10秒后停止worker 39 setTimeout(() => { 40 workerInstance.postMessage({ type: 'stop' }); 41 }, 10000); 42 this.message = 'success'; 43 }) 44 } 45 .height('100%') 46 .width('100%') 47 } 48} 49// [End worker_receive_child_thread_message]