1# Resident Task Development (Worker) 2 3This topic 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. 4 5The development process and example are outlined as follows: 6 71. 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". 8 9 You can also manually create Worker files. For details, see [Precautions for Worker](worker-introduction.md#precautions-for-worker). 10 112. Import the Worker module. 12 13 ```ts 14 // Index.ets 15 import { worker } from '@kit.ArkTS'; 16 ``` 17 183. In the host thread, call [constructor()](../reference/apis-arkts/js-apis-worker.md#constructor9) of ThreadWorker to create a Worker object. 19 20 ```ts 21 // Index.ets 22 const workerInstance: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 23 ``` 24 254. 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. 26 27 ```ts 28 // Index.ets 29 30 @Entry 31 @Component 32 struct Index { 33 build() { 34 Column() { 35 Text("Listener task") 36 .id('HelloWorld') 37 .fontSize(50) 38 .fontWeight(FontWeight.Bold) 39 .onClick(() => { 40 workerInstance.postMessage({type: 'start'}) 41 workerInstance.onmessage = (event) => { 42 console.info('The UI main thread receives a message:', event.data); 43 } 44 // Stop the Worker thread after 10 seconds. 45 setTimeout(() => { 46 workerInstance.postMessage({ type: 'stop' }); 47 }, 10000); 48 }) 49 } 50 .height('100%') 51 .width('100%') 52 } 53 } 54 ``` 55 <!-- @[worker_receive_child_thread_message](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ApplicationMultithreadingDevelopment/ApplicationMultithreading/entry/src/main/ets/managers/ResidentTaskGuide.ets) --> 56 575. 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. 58 59 ```ts 60 // Worker.ets 61 import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 62 const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 63 let isRunning = false; 64 workerPort.onmessage = (e: MessageEvents) => { 65 const type = e.data.type as string; 66 if (type === 'start') { 67 if (!isRunning) { 68 isRunning = true; 69 // Start a resident task. 70 performTask(); 71 } 72 } else if (type === 'stop') { 73 isRunning = false; 74 workerPort.close(); // Close the Worker thread. 75 } 76 } 77 // Simulate a resident task. 78 function performTask() { 79 if (isRunning) { 80 // Simulate a long-running task. 81 workerPort.postMessage('Worker is performing a task'); 82 // Execute the task again after 1 second. 83 setTimeout(performTask, 1000); 84 } 85 workerPort.postMessage('Worker is stop performing a task'); 86 } 87 ``` 88 <!-- @[worker_correspond_main_thread](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ApplicationMultithreadingDevelopment/ApplicationMultithreading/entry/src/main/ets/workers/Worker.ets) --> 89