harmony 鸿蒙Resident Task Development (Worker)

  • 2025-06-06
  • 浏览 (2)

Resident Task Development (Worker)

This section describes how to use Worker to execute a resident task. Worker continuously executes the task until it receives a termination command from the host thread.

The development process and example are outlined as follows:

  1. Create Worker with DevEco Studio. Specifically, in DevEco Studio, right-click anywhere in the {moduleName} directory and choose New > Worker to automatically generate the Worker template file and configuration information. In this example, we will create a Worker named “Worker”.

You can also manually create Worker files. For details, see Precautions for Worker.

  1. Import the Worker module.
   // Index.ets
   import { worker } from '@kit.ArkTS';
  1. In the host thread, call constructor() of ThreadWorker to create a Worker object.
   // Index.ets
   const workerInstance: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
  1. Enable the host thread to send messages. The host thread (UI main thread) sends ‘start’ to initiate a long-running task and receive messages from the Worker thread. When the task is no longer needed, the host thread sends ‘stop’ to terminate the task. In this example, the task is terminated after 10 seconds.
   // Index.ets
   
   @Entry
   @Component
   struct Index {
     build() {
       Column() {
         Text("Listener task")
           .id('HelloWorld')
           .fontSize(50)
           .fontWeight(FontWeight.Bold)
           .onClick(() => {
             workerInstance.postMessage({type: 'start'})
             workerInstance.onmessage = (event) => {
               console.info('The UI main thread receives a message:', event.data);
             }
             // Stop the Worker thread after 10 seconds.
             setTimeout(() => {
               workerInstance.postMessage({ type: 'stop' });
             }, 10000);
           })
       }
       .height('100%')
       .width('100%')
     }
   }
  1. Handle messages in the Worker thread. When receiving ‘start’ from the host thread, the Worker thread starts executing the long-running, non-periodic task and sends messages back to the host thread in real-time. When receiving ‘stop’, it terminates the task and sends a corresponding message back to the host thread.
   // Worker.ets
   import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
   const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
   let isRunning = false;
   workerPort.onmessage = (e: MessageEvents) => {
     const type = e.data.type as string;
     if (type === 'start') {
       if (!isRunning) {
         isRunning = true;
         // Start a resident task.
         performTask();
       }
     } else if (type === 'stop') {
       isRunning = false;
       workerPort.close(); // Close the Worker thread.
     }
   }
   // Simulate a resident task.
   function performTask() {
     if (isRunning) {
       // Simulate a long-running task.
       workerPort.postMessage('Worker is performing a task');
       // Execute the task again after 1 second.
       setTimeout(performTask, 1000);
     }
     workerPort.postMessage('Worker is stop performing a task');
   }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Configuring arkOptions in build-profile.json5

harmony 鸿蒙Asynchronous Lock

harmony 鸿蒙Ark Bytecode File Format

harmony 鸿蒙Naming Conventions for Ark Bytecode Functions

harmony 鸿蒙Ark Bytecode Fundamentals

harmony 鸿蒙Overview of Ark Bytecode

harmony 鸿蒙Shared Container

harmony 鸿蒙Asynchronous Waiting

harmony 鸿蒙ArkTS Cross-Language Interaction

0  赞