1# Resident Task Development (Worker) 2 3This section describes how to use a worker to execute a resident task. The worker continuously executes the task until the host thread sends a termination instruction. 4 5The development process and example are as follows: 6 71. DevEco Studio supports one-click Worker generation. Right-click any position in the {moduleName} directory and choose > New > Worker from the shortcut menu to automatically generate the Worker template file and configuration information. This section uses Worker as an example. 8 9 In addition, you can manually create a worker file. 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. The calling thread is the host thread. 19 20 ```ts 21 // Index.ets 22 const workerInstance: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 23 ``` 24 254. The host thread is the UI main thread. The host thread sends'start' to execute a long-running task and receive related messages returned by the sub-thread. When the task does not need to be executed, send'stop' to stop the task. In this example, the task is stopped 10 seconds later. 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 after 10 seconds. 45 setTimeout(() => { 46 workerInstance.postMessage({ type: 'stop' }); 47 }, 10000); 48 }) 49 } 50 .height('100%') 51 .width('100%') 52 } 53 } 54 ``` 55 565. When the Worker thread receives a start message from the host thread, the Worker thread starts to execute a task that runs irregularly for a long time and returns a message to the host thread in real time. When the received message is "stop," the task execution is ended and a corresponding message is returned to the host thread. 57 58 ```ts 59 // Worker.ets 60 import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 61 const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 62 let isRunning = false; 63 workerPort.onmessage = (e: MessageEvents) => { 64 const type = e.data.type as string; 65 if (type === 'start') { 66 if (!isRunning) { 67 isRunning = true; 68 // Start a resident task. 69 performTask(); 70 } 71 } else if (type === 'stop') { 72 isRunning = false; 73 workerPort.close (); // Close the worker. 74 } 75 } 76 // Simulate a resident task. 77 function performTask() { 78 if (isRunning) { 79 // Simulate a long-term task. 80 workerPort.postMessage('Worker is performing a task'); 81 // Execute the task again 1 second later. 82 setTimeout(performTask, 1000); 83 } 84 workerPort.postMessage('Worker is stop performing a task'); 85 } 86 ``` 87