1e41f4b71Sopenharmony_ci# @ohos.worker (Starting the Worker) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe worker thread is an independent thread running in parallel with the main thread. The thread that creates the worker thread is referred to as the host thread. The URL file passed in during worker creation is executed in the worker thread. The worker thread can process time-consuming operations, but cannot directly operate the UI. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciWith the **Worker** module, you can provide a multithreading environment for an application, so that the application can perform a time-consuming operation in a background thread. This greatly prevents a computing-intensive or high-latency task from blocking the running of the main thread. A **Worker** instance will not be proactively destroyed once it is created. It consumes resources to keep running. Therefore, you should call the API to terminate it in a timely manner. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe **Context** object of the worker thread is different from that of the main thread. The worker thread does not support UI operations. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciFor details about the precautions for using **Worker**, see [Precautions for Worker](../../arkts-utils/worker-introduction.md#precautions-for-worker). 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci> **NOTE** 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Modules to Import 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```ts 18e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 19e41f4b71Sopenharmony_ci``` 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Attributes 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 27e41f4b71Sopenharmony_ci| --------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 28e41f4b71Sopenharmony_ci| workerPort<sup>9+</sup> | [ThreadWorkerGlobalScope](#threadworkerglobalscope9) | Yes | Yes | Object of the worker thread used to communicate with the host thread. **Atomic service API**: This API can be used in atomic services since API version 11. | 29e41f4b71Sopenharmony_ci| parentPort<sup>(deprecated)</sup> | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscopedeprecated) | Yes | Yes | Object of the worker thread used to communicate with the host thread.<br>This attribute is supported since API version 7 and deprecated since API version 9.<br>You are advised to use **workerPort<sup>9+</sup>** instead.| 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## WorkerOptions 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciProvides options that can be set for the **Worker** instance to create. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci| Name| Type| Read-only| Optional| Description| 39e41f4b71Sopenharmony_ci| ---- | -------- | ---- | ---- | -------------- | 40e41f4b71Sopenharmony_ci| type | "classic" \| "module" | Yes | Yes| Mode in which the **Worker** instance executes the script. The **module** type is not supported yet. The default value is **classic**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 41e41f4b71Sopenharmony_ci| name | string | Yes | Yes| Name of the worker thread. The default value is **undefined**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 42e41f4b71Sopenharmony_ci| shared | boolean | Yes | Yes| Whether sharing of the **Worker** instance is enabled. Currently, sharing is not supported.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci## ThreadWorker<sup>9+</sup> 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciBefore using the following APIs, you must create a **ThreadWorker** instance. The **ThreadWorker** class inherits from [WorkerEventTarget](#workereventtarget9). 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci### constructor<sup>9+</sup> 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ciconstructor(scriptURL: string, options?: WorkerOptions) 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciA constructor used to create a **ThreadWorker** instance. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**Parameters** 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 61e41f4b71Sopenharmony_ci| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 62e41f4b71Sopenharmony_ci| scriptURL | string | Yes | URL of the worker thread file.<br>For details about the rules, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).| 63e41f4b71Sopenharmony_ci| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. | 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci**Error codes** 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci| ID| Error Message| 70e41f4b71Sopenharmony_ci| -------- | -------- | 71e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 72e41f4b71Sopenharmony_ci| 10200003 | Worker initialization failed. | 73e41f4b71Sopenharmony_ci| 10200007 | The worker file path is invalid. | 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**Example** 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciThe following code snippet shows how to load the worker thread file of the ability in the stage model. For details about how to use the library to load the worker thread file, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls). 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci```ts 80e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci// Two scenarios are involved. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci// Scenario 1: URL of the worker thread file: "entry/src/main/ets/workers/worker.ets" 85e41f4b71Sopenharmony_ciconst workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"}); 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci// Scenario 2: URL of the worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets" 88e41f4b71Sopenharmony_ciconst workerStageModel02 = new worker.ThreadWorker('phone/ets/ThreadFile/workers/worker.ets'); 89e41f4b71Sopenharmony_ci``` 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci### postMessage<sup>9+</sup> 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_cipostMessage(message: Object, transfer: ArrayBuffer[]): void 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciSends a message from the host thread to the worker thread by transferring object ownership. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**Parameters** 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 105e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------------------------------ | 106e41f4b71Sopenharmony_ci| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 107e41f4b71Sopenharmony_ci| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null.| 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**Error codes** 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci| ID| Error Message | 114e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 115e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 116e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 117e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**Example** 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci```ts 122e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_cilet buffer = new ArrayBuffer(8); 125e41f4b71Sopenharmony_ciworkerInstance.postMessage(buffer, [buffer]); 126e41f4b71Sopenharmony_ci``` 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci### postMessage<sup>9+</sup> 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_cipostMessage(message: Object, options?: PostMessageOptions): void 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ciSends a message from the host thread to the worker thread by transferring object ownership or copying data. 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**Parameters** 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 141e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 142e41f4b71Sopenharmony_ci| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 143e41f4b71Sopenharmony_ci| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the worker thread and becomes unavailable in the host thread. The objects are available only in the worker thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the worker thread by copying data.| 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci**Error codes** 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci| ID| Error Message | 150e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 151e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 152e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 153e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**Example** 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci```ts 158e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_cilet buffer = new ArrayBuffer(8); 163e41f4b71Sopenharmony_ciworkerInstance.postMessage(buffer, [buffer]); 164e41f4b71Sopenharmony_ci``` 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci### postMessageWithSharedSendable<sup>12+</sup> 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_cipostMessageWithSharedSendable(message: Object, transfer?: ArrayBuffer[]): void 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ciSends a message from the host thread to the worker thread. In the message, a sendable object is passed by reference, and a non-sendable object is passed by serialization. 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci**Parameters** 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 180e41f4b71Sopenharmony_ci| --------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 181e41f4b71Sopenharmony_ci| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable or sendable. For details about the supported sequenceable types, see [Sequenceable Data Types](#sequenceable-data-types). For details about the supported sendable types, see [Sendable Data Types](../../arkts-utils/arkts-sendable.md#sendable-data).| 182e41f4b71Sopenharmony_ci| transfer | ArrayBuffer[] | No | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null. The default value is an empty array.| 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**Error codes** 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci| ID| Error Message | 189e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 190e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 191e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 192e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci**Example** 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci<!--code_no_check--> 197e41f4b71Sopenharmony_ci```ts 198e41f4b71Sopenharmony_ci// index.ets 199e41f4b71Sopenharmony_ci// Create a SendableObject instance and pass it to the worker thread through the host thread. 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 202e41f4b71Sopenharmony_ciimport { SendableObject } from './sendable' 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/Worker.ets"); 205e41f4b71Sopenharmony_cilet object: SendableObject = new SendableObject(); 206e41f4b71Sopenharmony_ciworkerInstance.postMessageWithSharedSendable(object); 207e41f4b71Sopenharmony_ci``` 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci```ts 210e41f4b71Sopenharmony_ci// sendable.ets 211e41f4b71Sopenharmony_ci// Define SendableObject. 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci@Sendable 214e41f4b71Sopenharmony_ciexport class SendableObject { 215e41f4b71Sopenharmony_ci a:number = 45; 216e41f4b71Sopenharmony_ci} 217e41f4b71Sopenharmony_ci``` 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci<!--code_no_check--> 220e41f4b71Sopenharmony_ci```ts 221e41f4b71Sopenharmony_ci// The worker file path is entry/src/main/ets/workers/Worker.ets. 222e41f4b71Sopenharmony_ci// Worker.ets 223e41f4b71Sopenharmony_ci// Receive and access the data passed from the host thread to the worker thread. 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ciimport { SendableObject } from '../pages/sendable' 226e41f4b71Sopenharmony_ciimport { worker, ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 229e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 230e41f4b71Sopenharmony_ci let obj: SendableObject = e.data; 231e41f4b71Sopenharmony_ci console.info("sendable obj is: " + obj.a); 232e41f4b71Sopenharmony_ci} 233e41f4b71Sopenharmony_ci``` 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci### on<sup>9+</sup> 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_cion(type: string, listener: WorkerEventListener): void 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ciAdds an event listener for the worker thread. This API provides the same functionality as [addEventListener<sup>9+</sup>](#addeventlistener9). 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci**Parameters** 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 249e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------- | 250e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for. | 251e41f4b71Sopenharmony_ci| listener | [WorkerEventListener](#workereventlistener9) | Yes| Callback to invoke when an event of the specified type occurs.| 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**Error codes** 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| ID| Error Message | 258e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 259e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 260e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 261e41f4b71Sopenharmony_ci| 10200005 | The invoked API is not supported in workers. | 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci**Example** 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci```ts 266e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 267e41f4b71Sopenharmony_ciworkerInstance.on("alert", (e)=>{ 268e41f4b71Sopenharmony_ci console.log("alert listener callback"); 269e41f4b71Sopenharmony_ci}) 270e41f4b71Sopenharmony_ci``` 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci### once<sup>9+</sup> 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_cionce(type: string, listener: WorkerEventListener): void 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ciAdds an event listener for the worker thread and removes the event listener after it is invoked once. 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci**Parameters** 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 286e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------- | 287e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for. | 288e41f4b71Sopenharmony_ci| listener | [WorkerEventListener](#workereventlistener9) | Yes| Callback to invoke when an event of the specified type occurs.| 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**Error codes** 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci| ID| Error Message | 295e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 296e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 297e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 298e41f4b71Sopenharmony_ci| 10200005 | The invoked API is not supported in workers. | 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**Example** 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci```ts 303e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 304e41f4b71Sopenharmony_ciworkerInstance.once("alert", (e)=>{ 305e41f4b71Sopenharmony_ci console.log("alert listener callback"); 306e41f4b71Sopenharmony_ci}) 307e41f4b71Sopenharmony_ci``` 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci### off<sup>9+</sup> 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_cioff(type: string, listener?: WorkerEventListener): void 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ciRemoves an event listener for the worker thread. This API provides the same functionality as [removeEventListener<sup>9+</sup>](#removeeventlistener9). 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci**Parameters** 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 323e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------------- | 324e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event for which the event listener is to be removed. | 325e41f4b71Sopenharmony_ci| listener | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when the listener is removed.| 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci**Error codes** 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci| ID| Error Message | 332e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 333e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 334e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 335e41f4b71Sopenharmony_ci| 10200005 | The invoked API is not supported in workers. | 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci**Example** 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci```ts 340e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 341e41f4b71Sopenharmony_ci// Use on, once, or addEventListener to add a listener for the "alert" event, and use off to remove the listener. 342e41f4b71Sopenharmony_ciworkerInstance.off("alert"); 343e41f4b71Sopenharmony_ci``` 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci### registerGlobalCallObject<sup>11+</sup> 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ciregisterGlobalCallObject(instanceName: string, globalCallObject: Object): void 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciRegisters an object with the **ThreadWorker** instance of the host thread. In this way, the methods of the object can be called in the worker thread through [callGlobalCallObjectMethod](#callglobalcallobjectmethod11). 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci**Parameters** 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 358e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------------------------------ | 359e41f4b71Sopenharmony_ci| instanceName | string | Yes | Key used for registration, based on which the registered object is identified during method calling.| 360e41f4b71Sopenharmony_ci| globalCallObject | Object | Yes | Object to register. The **ThreadWorker** instance holds a strong reference to the object.| 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci**Error codes** 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci| ID| Error Message | 367e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 368e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 369e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci**Example** 372e41f4b71Sopenharmony_ci```ts 373e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 374e41f4b71Sopenharmony_ciclass TestObj { 375e41f4b71Sopenharmony_ci private message : string = "this is a message from TestObj" 376e41f4b71Sopenharmony_ci public getMessage() : string { 377e41f4b71Sopenharmony_ci return this.message; 378e41f4b71Sopenharmony_ci } 379e41f4b71Sopenharmony_ci public getMessageWithInput(str : string) : string { 380e41f4b71Sopenharmony_ci return this.message + " with input: " + str; 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci} 383e41f4b71Sopenharmony_cilet registerObj = new TestObj(); 384e41f4b71Sopenharmony_ci// Register registerObj with the ThreadWorker instance. 385e41f4b71Sopenharmony_ciworkerInstance.registerGlobalCallObject("myObj", registerObj); 386e41f4b71Sopenharmony_ciworkerInstance.postMessage("start worker") 387e41f4b71Sopenharmony_ci``` 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci### unregisterGlobalCallObject<sup>11+</sup> 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ciunregisterGlobalCallObject(instanceName?: string): void 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ciUnregisters an object with the **ThreadWorker** instance of the host thread. This API releases the strong reference between the ThreadWorker instance and the target object. No error is reported if no object is matched. 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci**Parameters** 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 402e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------------------------------ | 403e41f4b71Sopenharmony_ci| instanceName | string | No | Key used for registration. If this parameter is left blank, all registered objects registered in the **ThreadWorker** instance are unregistered.| 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci**Error codes** 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci| ID| Error Message | 410e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 411e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 412e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci**Example** 415e41f4b71Sopenharmony_ci```ts 416e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 417e41f4b71Sopenharmony_ciclass TestObj { 418e41f4b71Sopenharmony_ci private message : string = "this is a message from TestObj" 419e41f4b71Sopenharmony_ci public getMessage() : string { 420e41f4b71Sopenharmony_ci return this.message; 421e41f4b71Sopenharmony_ci } 422e41f4b71Sopenharmony_ci public getMessageWithInput(str : string) : string { 423e41f4b71Sopenharmony_ci return this.message + " with input: " + str; 424e41f4b71Sopenharmony_ci } 425e41f4b71Sopenharmony_ci} 426e41f4b71Sopenharmony_cilet registerObj = new TestObj(); 427e41f4b71Sopenharmony_ciworkerInstance.registerGlobalCallObject("myObj", registerObj); 428e41f4b71Sopenharmony_ci// Unregister the object. 429e41f4b71Sopenharmony_ciworkerInstance.unregisterGlobalCallObject("myObj"); 430e41f4b71Sopenharmony_ci// Unregister all objects from the ThreadWorker instance. 431e41f4b71Sopenharmony_ci//workerInstance.unregisterGlobalCallObject(); 432e41f4b71Sopenharmony_ciworkerInstance.postMessage("start worker") 433e41f4b71Sopenharmony_ci``` 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci### terminate<sup>9+</sup> 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_citerminate(): void 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ciTerminates the worker thread to stop it from receiving messages. 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**Error codes** 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci| ID| Error Message | 450e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 451e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci**Example** 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci```ts 456e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 457e41f4b71Sopenharmony_ciworkerInstance.terminate(); 458e41f4b71Sopenharmony_ci``` 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci### onexit<sup>9+</sup> 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_cionexit?: (code: number) => void 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ciCalled when the worker thread exits. The event handler is executed in the host thread. In the callback function, the **code** value is of the number type, where the value **1** indicates abnormal exit and **0** indicates normal exit. 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**Error codes** 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci| ID| Error Message | 476e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 477e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 478e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 479e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci**Example** 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ci```ts 484e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 485e41f4b71Sopenharmony_ciworkerInstance.onexit = (code) => { 486e41f4b71Sopenharmony_ci console.log("onexit"); 487e41f4b71Sopenharmony_ci} 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci// onexit is executed in either of the following ways: 490e41f4b71Sopenharmony_ci// Main thread 491e41f4b71Sopenharmony_ciworkerInstance.terminate(); 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci// Worker thread 494e41f4b71Sopenharmony_ci//workerPort.close() 495e41f4b71Sopenharmony_ci``` 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci### onerror<sup>9+</sup> 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_cionerror?: (err: ErrorEvent) => void 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ciCalled when an exception occurs during worker execution. The event handler is executed in the host thread. In the callback function, the **err** type is [ErrorEvent](#errorevent), indicating the received abnormal data. 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci**Error codes** 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci| ID| Error Message | 513e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 514e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 515e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 516e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci**Example** 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci```ts 521e41f4b71Sopenharmony_ciimport { worker, ErrorEvent } from '@kit.ArkTS'; 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 524e41f4b71Sopenharmony_ciworkerInstance.onerror = (err: ErrorEvent) => { 525e41f4b71Sopenharmony_ci console.log("onerror" + err.message); 526e41f4b71Sopenharmony_ci} 527e41f4b71Sopenharmony_ci``` 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci### onmessage<sup>9+</sup> 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_cionmessage?: (event: MessageEvents) => void 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ciCalled when the host thread receives a message sent by the worker thread through **workerPort.postMessage**. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvents](#messageevents9), indicating the received message data. 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci**Error codes** 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci| ID| Error Message | 545e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 546e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 547e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 548e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ci**Example** 551e41f4b71Sopenharmony_ci 552e41f4b71Sopenharmony_ci```ts 553e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 556e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 557e41f4b71Sopenharmony_ci // e: MessageEvents. The usage is as follows: 558e41f4b71Sopenharmony_ci // let data = e.data; 559e41f4b71Sopenharmony_ci console.log("onmessage"); 560e41f4b71Sopenharmony_ci} 561e41f4b71Sopenharmony_ci``` 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci### onmessageerror<sup>9+</sup> 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_cionmessageerror?: (event: MessageEvents) => void 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ciCalled when the worker thread receives a message that cannot be serialized. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvents](#messageevents9), indicating the received message data. 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci**Error codes** 575e41f4b71Sopenharmony_ci 576e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 577e41f4b71Sopenharmony_ci 578e41f4b71Sopenharmony_ci| ID| Error Message | 579e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 580e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 581e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 582e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci**Example** 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci```ts 587e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 590e41f4b71Sopenharmony_ciworkerInstance.onmessageerror = (err: MessageEvents) => { 591e41f4b71Sopenharmony_ci console.log("onmessageerror"); 592e41f4b71Sopenharmony_ci} 593e41f4b71Sopenharmony_ci``` 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci### addEventListener<sup>9+</sup> 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ciaddEventListener(type: string, listener: WorkerEventListener): void 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_ciAdds an event listener for the worker thread. This API provides the same functionality as [on<sup>9+</sup>](#on9). 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ci**Parameters** 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 608e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------- | 609e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for.| 610e41f4b71Sopenharmony_ci| listener | [WorkerEventListener](#workereventlistener9) | Yes | Callback to invoke when an event of the specified type occurs. | 611e41f4b71Sopenharmony_ci 612e41f4b71Sopenharmony_ci**Error codes** 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci| ID| Error Message | 617e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 618e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 619e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 620e41f4b71Sopenharmony_ci| 10200005 | The invoked API is not supported in workers. | 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ci**Example** 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci```ts 625e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 626e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 627e41f4b71Sopenharmony_ci console.log("alert listener callback"); 628e41f4b71Sopenharmony_ci}) 629e41f4b71Sopenharmony_ci``` 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci 632e41f4b71Sopenharmony_ci### removeEventListener<sup>9+</sup> 633e41f4b71Sopenharmony_ci 634e41f4b71Sopenharmony_ciremoveEventListener(type: string, callback?: WorkerEventListener): void 635e41f4b71Sopenharmony_ci 636e41f4b71Sopenharmony_ciRemoves an event listener for the worker thread. This API provides the same functionality as [off<sup>9+</sup>](#off9). 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci**Parameters** 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 645e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------------- | 646e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event for which the event listener is to be removed. | 647e41f4b71Sopenharmony_ci| callback | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when the listener is removed.| 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_ci**Error codes** 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci| ID| Error Message | 654e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 655e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 656e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci**Example** 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ci```ts 661e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 662e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 663e41f4b71Sopenharmony_ci console.log("alert listener callback"); 664e41f4b71Sopenharmony_ci}) 665e41f4b71Sopenharmony_ciworkerInstance.removeEventListener("alert"); 666e41f4b71Sopenharmony_ci``` 667e41f4b71Sopenharmony_ci 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci### dispatchEvent<sup>9+</sup> 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_cidispatchEvent(event: Event): boolean 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ciDispatches the event defined for the worker thread. 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 676e41f4b71Sopenharmony_ci 677e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci**Parameters** 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 682e41f4b71Sopenharmony_ci| ------ | --------------- | ---- | ---------------- | 683e41f4b71Sopenharmony_ci| event | [Event](#event) | Yes | Event to dispatch.| 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci**Return value** 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ci| Type | Description | 688e41f4b71Sopenharmony_ci| ------- | ------------------------------- | 689e41f4b71Sopenharmony_ci| boolean | Returns **true** if the event is dispatched; returns **false** otherwise.| 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci**Error codes** 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 694e41f4b71Sopenharmony_ci 695e41f4b71Sopenharmony_ci| ID| Error Message | 696e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 697e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 698e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 699e41f4b71Sopenharmony_ci 700e41f4b71Sopenharmony_ci**Example** 701e41f4b71Sopenharmony_ci 702e41f4b71Sopenharmony_ci```ts 703e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet. 706e41f4b71Sopenharmony_ci``` 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ciThe **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows: 709e41f4b71Sopenharmony_ci 710e41f4b71Sopenharmony_ci```ts 711e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 712e41f4b71Sopenharmony_ci 713e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 714e41f4b71Sopenharmony_ci 715e41f4b71Sopenharmony_ci// Usage 1: 716e41f4b71Sopenharmony_ciworkerInstance.on("alert_on", (e)=>{ 717e41f4b71Sopenharmony_ci console.log("alert listener callback"); 718e41f4b71Sopenharmony_ci}) 719e41f4b71Sopenharmony_ciworkerInstance.once("alert_once", (e)=>{ 720e41f4b71Sopenharmony_ci console.log("alert listener callback"); 721e41f4b71Sopenharmony_ci}) 722e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert_add", (e)=>{ 723e41f4b71Sopenharmony_ci console.log("alert listener callback"); 724e41f4b71Sopenharmony_ci}) 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci// The event listener created by once is removed after being executed once. 727e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is not supported yet. 728e41f4b71Sopenharmony_ci// The event listener created by on will not be proactively deleted. 729e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); 730e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); 731e41f4b71Sopenharmony_ci// The event listener created by addEventListener will not be proactively deleted. 732e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); 733e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_ci// Usage 2: 736e41f4b71Sopenharmony_ci// The event type can be customized, and the special types "message", "messageerror", and "error" exist. 737e41f4b71Sopenharmony_ci// When type = "message", the event handler defined by onmessage will also be executed. 738e41f4b71Sopenharmony_ci// When type = "messageerror", the event handler defined by onmessageerror will also be executed. 739e41f4b71Sopenharmony_ci// When type = "error", the event handler defined by onerror will also be executed. 740e41f4b71Sopenharmony_ci// removeEventListener or off can be used to remove an event listener that is created by addEventListener, on, or once. 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ciworkerInstance.addEventListener("message", (e)=>{ 743e41f4b71Sopenharmony_ci console.log("message listener callback"); 744e41f4b71Sopenharmony_ci}) 745e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 746e41f4b71Sopenharmony_ci console.log("onmessage : message listener callback"); 747e41f4b71Sopenharmony_ci} 748e41f4b71Sopenharmony_ci// When dispatchEvent is called to distribute the "message" event, the callback passed in addEventListener and onmessage will be invoked. 749e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"message", timeStamp:0}); 750e41f4b71Sopenharmony_ci``` 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci 753e41f4b71Sopenharmony_ci### removeAllListener<sup>9+</sup> 754e41f4b71Sopenharmony_ci 755e41f4b71Sopenharmony_ciremoveAllListener(): void 756e41f4b71Sopenharmony_ci 757e41f4b71Sopenharmony_ciRemoves all event listeners for the worker thread. 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 760e41f4b71Sopenharmony_ci 761e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 762e41f4b71Sopenharmony_ci 763e41f4b71Sopenharmony_ci**Error codes** 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ci| ID| Error Message | 768e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 769e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci**Example** 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci```ts 774e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 775e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 776e41f4b71Sopenharmony_ci console.log("alert listener callback"); 777e41f4b71Sopenharmony_ci}) 778e41f4b71Sopenharmony_ciworkerInstance.removeAllListener(); 779e41f4b71Sopenharmony_ci``` 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci## WorkerEventTarget<sup>9+</sup> 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ciProcesses worker listening events. 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci### addEventListener<sup>9+</sup> 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ciaddEventListener(type: string, listener: WorkerEventListener): void 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ciAdds an event listener for the worker thread. This API provides the same functionality as [on<sup>9+</sup>](#on9). 790e41f4b71Sopenharmony_ci 791e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 792e41f4b71Sopenharmony_ci 793e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_ci**Parameters** 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 798e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------- | 799e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for.| 800e41f4b71Sopenharmony_ci| listener | [WorkerEventListener](#workereventlistener9) | Yes | Callback to invoke when an event of the specified type occurs. | 801e41f4b71Sopenharmony_ci 802e41f4b71Sopenharmony_ci**Error codes** 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 805e41f4b71Sopenharmony_ci 806e41f4b71Sopenharmony_ci| ID| Error Message | 807e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 808e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 809e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 810e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ci**Example** 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci```ts 815e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 816e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 817e41f4b71Sopenharmony_ci console.log("alert listener callback"); 818e41f4b71Sopenharmony_ci}) 819e41f4b71Sopenharmony_ci``` 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci 822e41f4b71Sopenharmony_ci### removeEventListener<sup>9+</sup> 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ciremoveEventListener(type: string, callback?: WorkerEventListener): void 825e41f4b71Sopenharmony_ci 826e41f4b71Sopenharmony_ciRemoves an event listener for the worker thread. This API provides the same functionality as [off<sup>9+</sup>](#off9). 827e41f4b71Sopenharmony_ci 828e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 829e41f4b71Sopenharmony_ci 830e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 831e41f4b71Sopenharmony_ci 832e41f4b71Sopenharmony_ci**Parameters** 833e41f4b71Sopenharmony_ci 834e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 835e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------------- | 836e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event for which the event listener is to be removed. | 837e41f4b71Sopenharmony_ci| callback | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when the listener is removed.| 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci**Error codes** 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 842e41f4b71Sopenharmony_ci 843e41f4b71Sopenharmony_ci| ID| Error Message | 844e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 845e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 846e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci**Example** 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci```ts 851e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 852e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 853e41f4b71Sopenharmony_ci console.log("alert listener callback"); 854e41f4b71Sopenharmony_ci}) 855e41f4b71Sopenharmony_ciworkerInstance.removeEventListener("alert"); 856e41f4b71Sopenharmony_ci``` 857e41f4b71Sopenharmony_ci 858e41f4b71Sopenharmony_ci 859e41f4b71Sopenharmony_ci### dispatchEvent<sup>9+</sup> 860e41f4b71Sopenharmony_ci 861e41f4b71Sopenharmony_cidispatchEvent(event: Event): boolean 862e41f4b71Sopenharmony_ci 863e41f4b71Sopenharmony_ciDispatches the event defined for the worker thread. 864e41f4b71Sopenharmony_ci 865e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 866e41f4b71Sopenharmony_ci 867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci**Parameters** 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 872e41f4b71Sopenharmony_ci| ------ | --------------- | ---- | ---------------- | 873e41f4b71Sopenharmony_ci| event | [Event](#event) | Yes | Event to dispatch.| 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci**Return value** 876e41f4b71Sopenharmony_ci 877e41f4b71Sopenharmony_ci| Type | Description | 878e41f4b71Sopenharmony_ci| ------- | ------------------------------- | 879e41f4b71Sopenharmony_ci| boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.| 880e41f4b71Sopenharmony_ci 881e41f4b71Sopenharmony_ci**Error codes** 882e41f4b71Sopenharmony_ci 883e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 884e41f4b71Sopenharmony_ci 885e41f4b71Sopenharmony_ci| ID| Error Message | 886e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 887e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 888e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 889e41f4b71Sopenharmony_ci 890e41f4b71Sopenharmony_ci**Example** 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci```ts 893e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet. 896e41f4b71Sopenharmony_ci``` 897e41f4b71Sopenharmony_ci 898e41f4b71Sopenharmony_ciThe **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows: 899e41f4b71Sopenharmony_ci 900e41f4b71Sopenharmony_ci```ts 901e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ci// Usage 1: 906e41f4b71Sopenharmony_ciworkerInstance.on("alert_on", (e)=>{ 907e41f4b71Sopenharmony_ci console.log("alert listener callback"); 908e41f4b71Sopenharmony_ci}) 909e41f4b71Sopenharmony_ciworkerInstance.once("alert_once", (e)=>{ 910e41f4b71Sopenharmony_ci console.log("alert listener callback"); 911e41f4b71Sopenharmony_ci}) 912e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert_add", (e)=>{ 913e41f4b71Sopenharmony_ci console.log("alert listener callback"); 914e41f4b71Sopenharmony_ci}) 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci// The event listener created by once is removed after being executed once. 917e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is not supported yet. 918e41f4b71Sopenharmony_ci// The event listener created by on will not be proactively deleted. 919e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); 920e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); 921e41f4b71Sopenharmony_ci// The event listener created by addEventListener will not be proactively deleted. 922e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); 923e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci// Usage 2: 926e41f4b71Sopenharmony_ci// The event type can be customized, and the special types "message", "messageerror", and "error" exist. 927e41f4b71Sopenharmony_ci// When type = "message", the event handler defined by onmessage will also be executed. 928e41f4b71Sopenharmony_ci// When type = "messageerror", the event handler defined by onmessageerror will also be executed. 929e41f4b71Sopenharmony_ci// When type = "error", the event handler defined by onerror will also be executed. 930e41f4b71Sopenharmony_ci// removeEventListener or off can be used to remove an event listener that is created by addEventListener, on, or once. 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_ciworkerInstance.addEventListener("message", (e)=>{ 933e41f4b71Sopenharmony_ci console.log("message listener callback"); 934e41f4b71Sopenharmony_ci}) 935e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 936e41f4b71Sopenharmony_ci console.log("onmessage : message listener callback"); 937e41f4b71Sopenharmony_ci} 938e41f4b71Sopenharmony_ci// When dispatchEvent is called to distribute the "message" event, the callback passed in addEventListener and onmessage will be invoked. 939e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"message", timeStamp:0}); 940e41f4b71Sopenharmony_ci``` 941e41f4b71Sopenharmony_ci 942e41f4b71Sopenharmony_ci 943e41f4b71Sopenharmony_ci### removeAllListener<sup>9+</sup> 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ciremoveAllListener(): void 946e41f4b71Sopenharmony_ci 947e41f4b71Sopenharmony_ciRemoves all event listeners for the worker thread. 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci**Error codes** 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci| ID| Error Message | 958e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 959e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 960e41f4b71Sopenharmony_ci 961e41f4b71Sopenharmony_ci**Example** 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_ci```ts 964e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 965e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 966e41f4b71Sopenharmony_ci console.log("alert listener callback"); 967e41f4b71Sopenharmony_ci}) 968e41f4b71Sopenharmony_ciworkerInstance.removeAllListener(); 969e41f4b71Sopenharmony_ci``` 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci 972e41f4b71Sopenharmony_ci## ThreadWorkerGlobalScope<sup>9+</sup> 973e41f4b71Sopenharmony_ci 974e41f4b71Sopenharmony_ciImplements communication between the worker thread and the host thread. The **postMessage** API is used to send messages to the host thread, and the **close** API is used to terminate the worker thread. The **ThreadWorkerGlobalScope** class inherits from [GlobalScope<sup>9+</sup>](#globalscope9). 975e41f4b71Sopenharmony_ci 976e41f4b71Sopenharmony_ci### postMessage<sup>9+</sup> 977e41f4b71Sopenharmony_ci 978e41f4b71Sopenharmony_cipostMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 979e41f4b71Sopenharmony_ci 980e41f4b71Sopenharmony_ciSends a message from the worker thread to the host thread by transferring object ownership. 981e41f4b71Sopenharmony_ci 982e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci**Parameters** 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 989e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------------------------------ | 990e41f4b71Sopenharmony_ci| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 991e41f4b71Sopenharmony_ci| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null.| 992e41f4b71Sopenharmony_ci 993e41f4b71Sopenharmony_ci**Error codes** 994e41f4b71Sopenharmony_ci 995e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 996e41f4b71Sopenharmony_ci 997e41f4b71Sopenharmony_ci| ID| Error Message | 998e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 999e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1000e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1001e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ci**Example** 1004e41f4b71Sopenharmony_ci 1005e41f4b71Sopenharmony_ci```ts 1006e41f4b71Sopenharmony_ci// Main thread 1007e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1008e41f4b71Sopenharmony_ci 1009e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 1010e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 1011e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 1012e41f4b71Sopenharmony_ci console.log("receive data from worker.ets"); 1013e41f4b71Sopenharmony_ci} 1014e41f4b71Sopenharmony_ci``` 1015e41f4b71Sopenharmony_ci 1016e41f4b71Sopenharmony_ci```ts 1017e41f4b71Sopenharmony_ci// worker.ets 1018e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1019e41f4b71Sopenharmony_ci 1020e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1021e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 1022e41f4b71Sopenharmony_ci let buffer = new ArrayBuffer(8); 1023e41f4b71Sopenharmony_ci workerPort.postMessage(buffer, [buffer]); 1024e41f4b71Sopenharmony_ci} 1025e41f4b71Sopenharmony_ci``` 1026e41f4b71Sopenharmony_ci 1027e41f4b71Sopenharmony_ci### postMessage<sup>9+</sup> 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_cipostMessage(messageObject: Object, options?: PostMessageOptions): void 1030e41f4b71Sopenharmony_ci 1031e41f4b71Sopenharmony_ciSends a message from the worker thread to the host thread by transferring object ownership or copying data. 1032e41f4b71Sopenharmony_ci 1033e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1036e41f4b71Sopenharmony_ci 1037e41f4b71Sopenharmony_ci**Parameters** 1038e41f4b71Sopenharmony_ci 1039e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1040e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 1041e41f4b71Sopenharmony_ci| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 1042e41f4b71Sopenharmony_ci| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the host thread and becomes unavailable in the worker thread. The objects are available only in the host thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.| 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci**Error codes** 1045e41f4b71Sopenharmony_ci 1046e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1047e41f4b71Sopenharmony_ci 1048e41f4b71Sopenharmony_ci| ID| Error Message | 1049e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 1050e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1051e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1052e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 1053e41f4b71Sopenharmony_ci 1054e41f4b71Sopenharmony_ci**Example** 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci```ts 1057e41f4b71Sopenharmony_ci// Main thread 1058e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 1061e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 1062e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 1063e41f4b71Sopenharmony_ci console.log("receive data from worker.ets"); 1064e41f4b71Sopenharmony_ci} 1065e41f4b71Sopenharmony_ci``` 1066e41f4b71Sopenharmony_ci 1067e41f4b71Sopenharmony_ci```ts 1068e41f4b71Sopenharmony_ci// worker.ets 1069e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1070e41f4b71Sopenharmony_ci 1071e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1072e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 1073e41f4b71Sopenharmony_ci workerPort.postMessage("receive data from main thread"); 1074e41f4b71Sopenharmony_ci} 1075e41f4b71Sopenharmony_ci``` 1076e41f4b71Sopenharmony_ci 1077e41f4b71Sopenharmony_ci 1078e41f4b71Sopenharmony_ci### postMessageWithSharedSendable<sup>12+</sup> 1079e41f4b71Sopenharmony_ci 1080e41f4b71Sopenharmony_cipostMessageWithSharedSendable(message: Object, transfer?: ArrayBuffer[]): void 1081e41f4b71Sopenharmony_ci 1082e41f4b71Sopenharmony_ciSends a message from the worker thread to the host thread. In the message, a sendable object is passed by reference, and a non-sendable object is passed by serialization. 1083e41f4b71Sopenharmony_ci 1084e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1085e41f4b71Sopenharmony_ci 1086e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1087e41f4b71Sopenharmony_ci 1088e41f4b71Sopenharmony_ci**Parameters** 1089e41f4b71Sopenharmony_ci 1090e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1091e41f4b71Sopenharmony_ci| --------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 1092e41f4b71Sopenharmony_ci| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable or sendable. For details about the supported sequenceable types, see [Sequenceable Data Types](#sequenceable-data-types). For details about the supported sendable types, see [Sendable Data Types](../../arkts-utils/arkts-sendable.md#sendable-data).| 1093e41f4b71Sopenharmony_ci| transfer | ArrayBuffer[] | No | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null. The default value is an empty array.| 1094e41f4b71Sopenharmony_ci 1095e41f4b71Sopenharmony_ci**Error codes** 1096e41f4b71Sopenharmony_ci 1097e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1098e41f4b71Sopenharmony_ci 1099e41f4b71Sopenharmony_ci| ID| Error Message | 1100e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 1101e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1102e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1103e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 1104e41f4b71Sopenharmony_ci 1105e41f4b71Sopenharmony_ci**Example** 1106e41f4b71Sopenharmony_ci 1107e41f4b71Sopenharmony_ci<!--code_no_check--> 1108e41f4b71Sopenharmony_ci```ts 1109e41f4b71Sopenharmony_ci// The worker file path is entry/src/main/ets/workers/Worker.ets. 1110e41f4b71Sopenharmony_ci// Worker.ets 1111e41f4b71Sopenharmony_ci// Create a SendableObject instance and pass it to the host thread through the worker thread. 1112e41f4b71Sopenharmony_ci 1113e41f4b71Sopenharmony_ciimport { SendableObject } from '../pages/sendable' 1114e41f4b71Sopenharmony_ciimport { worker, ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 1117e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 1118e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(); 1119e41f4b71Sopenharmony_ci workerPort.postMessageWithSharedSendable(object); 1120e41f4b71Sopenharmony_ci} 1121e41f4b71Sopenharmony_ci``` 1122e41f4b71Sopenharmony_ci 1123e41f4b71Sopenharmony_ci```ts 1124e41f4b71Sopenharmony_ci// sendable.ets 1125e41f4b71Sopenharmony_ci// Define SendableObject. 1126e41f4b71Sopenharmony_ci 1127e41f4b71Sopenharmony_ci@Sendable 1128e41f4b71Sopenharmony_ciexport class SendableObject { 1129e41f4b71Sopenharmony_ci a:number = 45; 1130e41f4b71Sopenharmony_ci} 1131e41f4b71Sopenharmony_ci``` 1132e41f4b71Sopenharmony_ci 1133e41f4b71Sopenharmony_ci<!--code_no_check--> 1134e41f4b71Sopenharmony_ci```ts 1135e41f4b71Sopenharmony_ci// Index.ets 1136e41f4b71Sopenharmony_ci// Receive the data passed from the worker thread to the host thread and access its properties. 1137e41f4b71Sopenharmony_ci 1138e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1139e41f4b71Sopenharmony_ciimport { SendableObject } from './sendable' 1140e41f4b71Sopenharmony_ci 1141e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/Worker.ets"); 1142e41f4b71Sopenharmony_ciworkerInstance.postMessage(1); 1143e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents) => { 1144e41f4b71Sopenharmony_ci let obj: SendableObject = e.data; 1145e41f4b71Sopenharmony_ci console.info("sendable index obj is: " + obj.a); 1146e41f4b71Sopenharmony_ci} 1147e41f4b71Sopenharmony_ci``` 1148e41f4b71Sopenharmony_ci 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci### callGlobalCallObjectMethod<sup>11+</sup> 1151e41f4b71Sopenharmony_ci 1152e41f4b71Sopenharmony_cicallGlobalCallObjectMethod(instanceName: string, methodName: string, timeout: number, ...args: Object[]): Object 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ciCalls a method of an object registered with the host thread. This API is called by the worker thread. The invoking is synchronous for the worker thread and asynchronous for the host thread. The return value is transferred through serialization. 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1157e41f4b71Sopenharmony_ci 1158e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1159e41f4b71Sopenharmony_ci 1160e41f4b71Sopenharmony_ci**Parameters** 1161e41f4b71Sopenharmony_ci 1162e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1163e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 1164e41f4b71Sopenharmony_ci| instanceName | string | Yes | Key used for registration. It is used to search for the object in the host thread.| 1165e41f4b71Sopenharmony_ci| methodName | string | Yes| Name of the method to call. Note that the method cannot be modified by async or generator, or return results asynchronously by using the asynchronous mechanism at the bottom layer. Otherwise, an exception is thrown.| 1166e41f4b71Sopenharmony_ci| timeout | number | Yes| Maximum duration that the current synchronous invoking waits, in ms. The value is an integer ranging from 1 to 5000. The value **0** means that the 5000 ms duration is used.| 1167e41f4b71Sopenharmony_ci| args | Object[] | No| Array of parameters in the method.| 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_ci**Return value** 1170e41f4b71Sopenharmony_ci 1171e41f4b71Sopenharmony_ci| Type | Description | 1172e41f4b71Sopenharmony_ci| ------------------------------------- | ------------------------------- | 1173e41f4b71Sopenharmony_ci| Object | Return value of the method in the host thread. The return value must be serializable. For details, see [Sequenceable Data Types](#sequenceable-data-types).| 1174e41f4b71Sopenharmony_ci 1175e41f4b71Sopenharmony_ci**Error codes** 1176e41f4b71Sopenharmony_ci 1177e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1178e41f4b71Sopenharmony_ci 1179e41f4b71Sopenharmony_ci| ID| Error Message | 1180e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | 1181e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1182e41f4b71Sopenharmony_ci| 10200004 | Worker instance is not running. | 1183e41f4b71Sopenharmony_ci| 10200006 | An exception occurred during serialization. | 1184e41f4b71Sopenharmony_ci| 10200019 | The globalCallObject is not registered. | 1185e41f4b71Sopenharmony_ci| 10200020 | The method to be called is not callable or is an async method or a generator. | 1186e41f4b71Sopenharmony_ci| 10200021 | The global call exceeds the timeout. | 1187e41f4b71Sopenharmony_ci 1188e41f4b71Sopenharmony_ci**Example** 1189e41f4b71Sopenharmony_ci```ts 1190e41f4b71Sopenharmony_ci// worker.ets 1191e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1192e41f4b71Sopenharmony_ci 1193e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1194e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 1195e41f4b71Sopenharmony_ci try { 1196e41f4b71Sopenharmony_ci // The method to call does not carry an input parameter. 1197e41f4b71Sopenharmony_ci let res : string = workerPort.callGlobalCallObjectMethod("myObj", "getMessage", 0) as string; 1198e41f4b71Sopenharmony_ci console.info("worker:", res) // worker: this is a message from TestObj 1199e41f4b71Sopenharmony_ci } catch (error) { 1200e41f4b71Sopenharmony_ci // Exception handling. 1201e41f4b71Sopenharmony_ci console.error("worker: error code is " + error.code + " error message is " + error.message); 1202e41f4b71Sopenharmony_ci } 1203e41f4b71Sopenharmony_ci try { 1204e41f4b71Sopenharmony_ci // The method to call carries input parameters. 1205e41f4b71Sopenharmony_ci let res : string = workerPort.callGlobalCallObjectMethod("myObj", "getMessageWithInput", 0, "hello there!") as string; 1206e41f4b71Sopenharmony_ci console.info("worker:", res) //worker: this is a message from TestObj with input: hello there! 1207e41f4b71Sopenharmony_ci } catch (error) { 1208e41f4b71Sopenharmony_ci // Exception handling. 1209e41f4b71Sopenharmony_ci console.error("worker: error code is " + error.code + " error message is " + error.message); 1210e41f4b71Sopenharmony_ci } 1211e41f4b71Sopenharmony_ci} 1212e41f4b71Sopenharmony_ci``` 1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_ci### close<sup>9+</sup> 1215e41f4b71Sopenharmony_ci 1216e41f4b71Sopenharmony_ciclose(): void 1217e41f4b71Sopenharmony_ci 1218e41f4b71Sopenharmony_ciTerminates the worker thread to stop it from receiving messages. 1219e41f4b71Sopenharmony_ci 1220e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1221e41f4b71Sopenharmony_ci 1222e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1223e41f4b71Sopenharmony_ci 1224e41f4b71Sopenharmony_ci**Error codes** 1225e41f4b71Sopenharmony_ci 1226e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1227e41f4b71Sopenharmony_ci 1228e41f4b71Sopenharmony_ci| ID| Error Message | 1229e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 1230e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1231e41f4b71Sopenharmony_ci 1232e41f4b71Sopenharmony_ci**Example** 1233e41f4b71Sopenharmony_ci 1234e41f4b71Sopenharmony_ci```ts 1235e41f4b71Sopenharmony_ci// Main thread 1236e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1237e41f4b71Sopenharmony_ci 1238e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 1239e41f4b71Sopenharmony_ci``` 1240e41f4b71Sopenharmony_ci 1241e41f4b71Sopenharmony_ci```ts 1242e41f4b71Sopenharmony_ci// worker.ets 1243e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1244e41f4b71Sopenharmony_ci 1245e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1246e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 1247e41f4b71Sopenharmony_ci workerPort.close() 1248e41f4b71Sopenharmony_ci} 1249e41f4b71Sopenharmony_ci``` 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ci 1252e41f4b71Sopenharmony_ci### onmessage<sup>9+</sup> 1253e41f4b71Sopenharmony_ci 1254e41f4b71Sopenharmony_cionmessage?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void 1255e41f4b71Sopenharmony_ci 1256e41f4b71Sopenharmony_ciCalled when the worker thread receives a message sent by the host thread through **postMessage**. The event handler is executed in the worker thread. In the callback function, **this** indicates the caller's [ThreadWorkerGlobalScope](#threadworkerglobalscope9), and the **ev** type is [MessageEvents](#messageevents9), indicating the received message data. 1257e41f4b71Sopenharmony_ci 1258e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1259e41f4b71Sopenharmony_ci 1260e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1261e41f4b71Sopenharmony_ci 1262e41f4b71Sopenharmony_ci**Error codes** 1263e41f4b71Sopenharmony_ci 1264e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1265e41f4b71Sopenharmony_ci 1266e41f4b71Sopenharmony_ci| ID| Error Message | 1267e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 1268e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1269e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1270e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci**Example** 1273e41f4b71Sopenharmony_ci 1274e41f4b71Sopenharmony_ci```ts 1275e41f4b71Sopenharmony_ci// Main thread 1276e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 1279e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 1280e41f4b71Sopenharmony_ci``` 1281e41f4b71Sopenharmony_ci 1282e41f4b71Sopenharmony_ci```ts 1283e41f4b71Sopenharmony_ci// worker.ets 1284e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1285e41f4b71Sopenharmony_ci 1286e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1287e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 1288e41f4b71Sopenharmony_ci console.log("receive main thread message"); 1289e41f4b71Sopenharmony_ci} 1290e41f4b71Sopenharmony_ci``` 1291e41f4b71Sopenharmony_ci 1292e41f4b71Sopenharmony_ci 1293e41f4b71Sopenharmony_ci### onmessageerror<sup>9+</sup> 1294e41f4b71Sopenharmony_ci 1295e41f4b71Sopenharmony_cionmessageerror?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void 1296e41f4b71Sopenharmony_ci 1297e41f4b71Sopenharmony_ciCalled when the worker thread receives a message that cannot be deserialized. The event handler is executed in the worker thread. In the callback function, **this** indicates the caller's [ThreadWorkerGlobalScope](#threadworkerglobalscope9), and the **ev** type is [MessageEvents](#messageevents9), indicating the received message data. 1298e41f4b71Sopenharmony_ci 1299e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1300e41f4b71Sopenharmony_ci 1301e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ci**Error codes** 1304e41f4b71Sopenharmony_ci 1305e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1306e41f4b71Sopenharmony_ci 1307e41f4b71Sopenharmony_ci| ID| Error Message | 1308e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 1309e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1310e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1311e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 1312e41f4b71Sopenharmony_ci 1313e41f4b71Sopenharmony_ci**Example** 1314e41f4b71Sopenharmony_ci 1315e41f4b71Sopenharmony_ci```ts 1316e41f4b71Sopenharmony_ci// Main thread 1317e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1318e41f4b71Sopenharmony_ci 1319e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 1320e41f4b71Sopenharmony_ci``` 1321e41f4b71Sopenharmony_ci 1322e41f4b71Sopenharmony_ci```ts 1323e41f4b71Sopenharmony_ci// worker.ets 1324e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1325e41f4b71Sopenharmony_ci 1326e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1327e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (err: MessageEvents) => { 1328e41f4b71Sopenharmony_ci console.log("worker.ets onmessageerror"); 1329e41f4b71Sopenharmony_ci} 1330e41f4b71Sopenharmony_ci``` 1331e41f4b71Sopenharmony_ci 1332e41f4b71Sopenharmony_ci 1333e41f4b71Sopenharmony_ci## WorkerEventListener<sup>9+</sup> 1334e41f4b71Sopenharmony_ci 1335e41f4b71Sopenharmony_ciImplements event listening. 1336e41f4b71Sopenharmony_ci 1337e41f4b71Sopenharmony_ci### (event: Event)<sup>9+</sup> 1338e41f4b71Sopenharmony_ci 1339e41f4b71Sopenharmony_ci(event: Event): void | Promise<void> 1340e41f4b71Sopenharmony_ci 1341e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1342e41f4b71Sopenharmony_ci 1343e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1344e41f4b71Sopenharmony_ci 1345e41f4b71Sopenharmony_ci**Parameters** 1346e41f4b71Sopenharmony_ci 1347e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1348e41f4b71Sopenharmony_ci| ------ | --------------- | ---- | -------------- | 1349e41f4b71Sopenharmony_ci| event | [Event](#event) | Yes | Event class for the callback to invoke.| 1350e41f4b71Sopenharmony_ci 1351e41f4b71Sopenharmony_ci**Return value** 1352e41f4b71Sopenharmony_ci 1353e41f4b71Sopenharmony_ci| Type | Description | 1354e41f4b71Sopenharmony_ci| ------------------------------------- | ------------------------------- | 1355e41f4b71Sopenharmony_ci| void \| Promise<void> | Returns no value or returns a **Promise**.| 1356e41f4b71Sopenharmony_ci 1357e41f4b71Sopenharmony_ci**Error codes** 1358e41f4b71Sopenharmony_ci 1359e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1360e41f4b71Sopenharmony_ci 1361e41f4b71Sopenharmony_ci| ID| Error Message | 1362e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 1363e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1364e41f4b71Sopenharmony_ci| 10200004 | The Worker instance is not running. | 1365e41f4b71Sopenharmony_ci| 10200005 | The called API is not supported in the worker thread. | 1366e41f4b71Sopenharmony_ci 1367e41f4b71Sopenharmony_ci**Example** 1368e41f4b71Sopenharmony_ci 1369e41f4b71Sopenharmony_ci```ts 1370e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets"); 1371e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 1372e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1373e41f4b71Sopenharmony_ci}) 1374e41f4b71Sopenharmony_ci``` 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci 1377e41f4b71Sopenharmony_ci## GlobalScope<sup>9+</sup> 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_ciImplements the running environment of the worker thread. The **GlobalScope** class inherits from [WorkerEventTarget](#workereventtarget9). 1380e41f4b71Sopenharmony_ci 1381e41f4b71Sopenharmony_ci### Attributes 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1386e41f4b71Sopenharmony_ci 1387e41f4b71Sopenharmony_ci| Name| Type | Readable| Writable| Description | 1388e41f4b71Sopenharmony_ci| ---- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------- | 1389e41f4b71Sopenharmony_ci| name | string | Yes | No | **Worker** instance specified when there is a new **Worker** instance.| 1390e41f4b71Sopenharmony_ci| self | [GlobalScope](#globalscope9) & typeof globalThis | Yes | No | **GlobalScope** itself. | 1391e41f4b71Sopenharmony_ci 1392e41f4b71Sopenharmony_ci 1393e41f4b71Sopenharmony_ci### onerror<sup>9+</sup> 1394e41f4b71Sopenharmony_ci 1395e41f4b71Sopenharmony_cionerror?: (ev: ErrorEvent) => void 1396e41f4b71Sopenharmony_ci 1397e41f4b71Sopenharmony_ciCalled when an exception occurs during worker execution. The event handler is executed in the worker thread. In the callback function, the **ev** type is [ErrorEvent](#errorevent), indicating the received abnormal data. 1398e41f4b71Sopenharmony_ci 1399e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1400e41f4b71Sopenharmony_ci 1401e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1402e41f4b71Sopenharmony_ci 1403e41f4b71Sopenharmony_ci**Example** 1404e41f4b71Sopenharmony_ci 1405e41f4b71Sopenharmony_ci```ts 1406e41f4b71Sopenharmony_ci// Main thread 1407e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1408e41f4b71Sopenharmony_ci 1409e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets") 1410e41f4b71Sopenharmony_ci``` 1411e41f4b71Sopenharmony_ci 1412e41f4b71Sopenharmony_ci```ts 1413e41f4b71Sopenharmony_ci// worker.ets 1414e41f4b71Sopenharmony_ciimport { worker, ErrorEvent } from '@kit.ArkTS'; 1415e41f4b71Sopenharmony_ci 1416e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort 1417e41f4b71Sopenharmony_ciworkerPort.onerror = (err: ErrorEvent) => { 1418e41f4b71Sopenharmony_ci console.log("worker.ets onerror" + err.message) 1419e41f4b71Sopenharmony_ci} 1420e41f4b71Sopenharmony_ci``` 1421e41f4b71Sopenharmony_ci 1422e41f4b71Sopenharmony_ci## MessageEvents<sup>9+</sup> 1423e41f4b71Sopenharmony_ci 1424e41f4b71Sopenharmony_ciHolds the data transferred between worker threads. 1425e41f4b71Sopenharmony_ci 1426e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1427e41f4b71Sopenharmony_ci 1428e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1429e41f4b71Sopenharmony_ci 1430e41f4b71Sopenharmony_ci| Name| Type| Readable| Writable| Description | 1431e41f4b71Sopenharmony_ci| ---- | ---- | ---- | ---- | ------------------ | 1432e41f4b71Sopenharmony_ci| data | any | Yes | No | Data transferred between threads.| 1433e41f4b71Sopenharmony_ci 1434e41f4b71Sopenharmony_ci## RestrictedWorker<sup>11+</sup> 1435e41f4b71Sopenharmony_ci 1436e41f4b71Sopenharmony_ciThe RestrictedWorker class inherits [ThreadWorker<sup>9+</sup>](#threadworker9) and supports all APIs in **ThreadWorker**. 1437e41f4b71Sopenharmony_ci**RestrictedWorker** provides a restricted environment for running the worker thread. In this environment, only the **Worker** module can be imported. 1438e41f4b71Sopenharmony_ci 1439e41f4b71Sopenharmony_ci### constructor<sup>11+</sup> 1440e41f4b71Sopenharmony_ci 1441e41f4b71Sopenharmony_ciconstructor(scriptURL: string, options?: WorkerOptions) 1442e41f4b71Sopenharmony_ci 1443e41f4b71Sopenharmony_ciA constructor used to create a **RestrictedWorker** instance. Before using the following APIs, you must create a **RestrictedWorker** instance. 1444e41f4b71Sopenharmony_ci 1445e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1446e41f4b71Sopenharmony_ci 1447e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1448e41f4b71Sopenharmony_ci 1449e41f4b71Sopenharmony_ci**Parameters** 1450e41f4b71Sopenharmony_ci 1451e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1452e41f4b71Sopenharmony_ci| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1453e41f4b71Sopenharmony_ci| scriptURL | string | Yes | URL of the worker thread file. For details about the rules, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).| 1454e41f4b71Sopenharmony_ci| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **RestrictedWorker** instance. | 1455e41f4b71Sopenharmony_ci 1456e41f4b71Sopenharmony_ci**Error codes** 1457e41f4b71Sopenharmony_ci 1458e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1459e41f4b71Sopenharmony_ci 1460e41f4b71Sopenharmony_ci| ID| Error Message| 1461e41f4b71Sopenharmony_ci| -------- | -------- | 1462e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1463e41f4b71Sopenharmony_ci| 10200003 | Worker initialization failure. | 1464e41f4b71Sopenharmony_ci| 10200007 | The worker file patch is invalid path. | 1465e41f4b71Sopenharmony_ci 1466e41f4b71Sopenharmony_ci**Example** 1467e41f4b71Sopenharmony_ci 1468e41f4b71Sopenharmony_ciThe following code snippet shows how to load the worker thread file of the ability in the stage model. For details about how to use the library to load the worker thread file, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls). 1469e41f4b71Sopenharmony_ci 1470e41f4b71Sopenharmony_ciOnly the **Worker** module can be imported to the restricted worker thread file. Other APIs cannot be imported. The following is sample code: 1471e41f4b71Sopenharmony_ci 1472e41f4b71Sopenharmony_ci```ts 1473e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ci// Two scenarios are involved. 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ci// Scenario 1: URL of the worker thread file: "entry/src/main/ets/workers/worker.ets" 1478e41f4b71Sopenharmony_ciconst workerStageModel01 = new worker.RestrictedWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"}); 1479e41f4b71Sopenharmony_ci 1480e41f4b71Sopenharmony_ci// Scenario 2: URL of the worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets" 1481e41f4b71Sopenharmony_ciconst workerStageModel02 = new worker.RestrictedWorker('phone/ets/ThreadFile/workers/worker.ets'); 1482e41f4b71Sopenharmony_ci``` 1483e41f4b71Sopenharmony_ci 1484e41f4b71Sopenharmony_ci```ts 1485e41f4b71Sopenharmony_ci// Restricted worker thread file 1486e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 1487e41f4b71Sopenharmony_ci 1488e41f4b71Sopenharmony_ci//import { process } from '@kit.ArkTS'; // Only worker APIs can be imported to the restricted worker thread file. 1489e41f4b71Sopenharmony_ci 1490e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 1491e41f4b71Sopenharmony_ci 1492e41f4b71Sopenharmony_ciworkerPort.onmessage = (e : MessageEvents) : void => { 1493e41f4b71Sopenharmony_ci console.info("worker:: This is worker thread.") 1494e41f4b71Sopenharmony_ci // console.info("worker:: worker tid: "+ process.tid) // Run process.tid. The main thread reports the corresponding error. 1495e41f4b71Sopenharmony_ci} 1496e41f4b71Sopenharmony_ci``` 1497e41f4b71Sopenharmony_ci 1498e41f4b71Sopenharmony_ci## Worker<sup>(deprecated)</sup> 1499e41f4b71Sopenharmony_ci 1500e41f4b71Sopenharmony_ci 1501e41f4b71Sopenharmony_ciBefore using the following APIs, you must create a **Worker** instance. The **Worker** class inherits from [EventTarget](#eventtargetdeprecated). 1502e41f4b71Sopenharmony_ci 1503e41f4b71Sopenharmony_ci> **NOTE**<br> 1504e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker<sup>9+</sup>](#threadworker9) instead. 1505e41f4b71Sopenharmony_ci 1506e41f4b71Sopenharmony_ci### constructor<sup>(deprecated)</sup> 1507e41f4b71Sopenharmony_ci 1508e41f4b71Sopenharmony_ciconstructor(scriptURL: string, options?: WorkerOptions) 1509e41f4b71Sopenharmony_ci 1510e41f4b71Sopenharmony_ciA constructor used to create a **Worker** instance. 1511e41f4b71Sopenharmony_ci 1512e41f4b71Sopenharmony_ci> **NOTE**<br> 1513e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.constructor<sup>9+</sup>](#constructor9) instead. 1514e41f4b71Sopenharmony_ci 1515e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1516e41f4b71Sopenharmony_ci 1517e41f4b71Sopenharmony_ci**Parameters** 1518e41f4b71Sopenharmony_ci 1519e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1520e41f4b71Sopenharmony_ci| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1521e41f4b71Sopenharmony_ci| scriptURL | string | Yes | URL of the worker thread file. For details about the rules, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).| 1522e41f4b71Sopenharmony_ci| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. | 1523e41f4b71Sopenharmony_ci 1524e41f4b71Sopenharmony_ci**Example** 1525e41f4b71Sopenharmony_ci 1526e41f4b71Sopenharmony_ciThe following code snippet shows how to load the worker thread file of the ability in the stage model. For details about how to use the library to load the worker thread file, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls). 1527e41f4b71Sopenharmony_ci 1528e41f4b71Sopenharmony_ci 1529e41f4b71Sopenharmony_ci```ts 1530e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1531e41f4b71Sopenharmony_ci 1532e41f4b71Sopenharmony_ci// Two scenarios are involved. 1533e41f4b71Sopenharmony_ci 1534e41f4b71Sopenharmony_ci// Scenario 1: URL of the worker thread file: "entry/src/main/ets/workers/worker.ets" 1535e41f4b71Sopenharmony_ciconst workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"}); 1536e41f4b71Sopenharmony_ci 1537e41f4b71Sopenharmony_ci// Scenario 2: URL of the worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets" 1538e41f4b71Sopenharmony_ciconst workerStageModel02 = new worker.ThreadWorker('phone/ets/ThreadFile/workers/worker.ets'); 1539e41f4b71Sopenharmony_ci``` 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_ci### postMessage<sup>(deprecated)</sup> 1542e41f4b71Sopenharmony_ci 1543e41f4b71Sopenharmony_cipostMessage(message: Object, transfer: ArrayBuffer[]): void 1544e41f4b71Sopenharmony_ci 1545e41f4b71Sopenharmony_ciSends a message from the host thread to the worker thread by transferring object ownership. 1546e41f4b71Sopenharmony_ci 1547e41f4b71Sopenharmony_ci> **NOTE**<br> 1548e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage<sup>9+</sup>](#postmessage9) instead. 1549e41f4b71Sopenharmony_ci 1550e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1551e41f4b71Sopenharmony_ci 1552e41f4b71Sopenharmony_ci**Parameters** 1553e41f4b71Sopenharmony_ci 1554e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1555e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------------------------------ | 1556e41f4b71Sopenharmony_ci| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 1557e41f4b71Sopenharmony_ci| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null.| 1558e41f4b71Sopenharmony_ci 1559e41f4b71Sopenharmony_ci**Example** 1560e41f4b71Sopenharmony_ci 1561e41f4b71Sopenharmony_ci```ts 1562e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1563e41f4b71Sopenharmony_ci 1564e41f4b71Sopenharmony_cilet buffer = new ArrayBuffer(8); 1565e41f4b71Sopenharmony_ciworkerInstance.postMessage(buffer, [buffer]); 1566e41f4b71Sopenharmony_ci``` 1567e41f4b71Sopenharmony_ci 1568e41f4b71Sopenharmony_ci### postMessage<sup>(deprecated)</sup> 1569e41f4b71Sopenharmony_ci 1570e41f4b71Sopenharmony_cipostMessage(message: Object, options?: PostMessageOptions): void 1571e41f4b71Sopenharmony_ci 1572e41f4b71Sopenharmony_ciSends a message from the host thread to the worker thread by transferring object ownership or copying data. 1573e41f4b71Sopenharmony_ci 1574e41f4b71Sopenharmony_ci> **NOTE**<br> 1575e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage<sup>9+</sup>](#postmessage9-1) instead. 1576e41f4b71Sopenharmony_ci 1577e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1578e41f4b71Sopenharmony_ci 1579e41f4b71Sopenharmony_ci**Parameters** 1580e41f4b71Sopenharmony_ci 1581e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1582e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 1583e41f4b71Sopenharmony_ci| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 1584e41f4b71Sopenharmony_ci| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the worker thread and becomes unavailable in the host thread. The objects are available only in the worker thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the worker thread by copying data.| 1585e41f4b71Sopenharmony_ci 1586e41f4b71Sopenharmony_ci**Example** 1587e41f4b71Sopenharmony_ci 1588e41f4b71Sopenharmony_ci```ts 1589e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1590e41f4b71Sopenharmony_ci 1591e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 1592e41f4b71Sopenharmony_ci 1593e41f4b71Sopenharmony_cilet buffer = new ArrayBuffer(8); 1594e41f4b71Sopenharmony_ciworkerInstance.postMessage(buffer, [buffer]); 1595e41f4b71Sopenharmony_ci``` 1596e41f4b71Sopenharmony_ci 1597e41f4b71Sopenharmony_ci 1598e41f4b71Sopenharmony_ci### on<sup>(deprecated)</sup> 1599e41f4b71Sopenharmony_ci 1600e41f4b71Sopenharmony_cion(type: string, listener: EventListener): void 1601e41f4b71Sopenharmony_ci 1602e41f4b71Sopenharmony_ciAdds an event listener for the worker thread. This API provides the same functionality as [addEventListener<sup>(deprecated)</sup>](#addeventlistenerdeprecated). 1603e41f4b71Sopenharmony_ci 1604e41f4b71Sopenharmony_ci> **NOTE**<br> 1605e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.on<sup>9+</sup>](#on9) instead. 1606e41f4b71Sopenharmony_ci 1607e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1608e41f4b71Sopenharmony_ci 1609e41f4b71Sopenharmony_ci**Parameters** 1610e41f4b71Sopenharmony_ci 1611e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1612e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ---------------- | 1613e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for.| 1614e41f4b71Sopenharmony_ci| listener | [EventListener](#eventlistenerdeprecated) | Yes | Callback to invoke when an event of the specified type occurs. | 1615e41f4b71Sopenharmony_ci 1616e41f4b71Sopenharmony_ci**Example** 1617e41f4b71Sopenharmony_ci 1618e41f4b71Sopenharmony_ci```ts 1619e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1620e41f4b71Sopenharmony_ciworkerInstance.on("alert", (e)=>{ 1621e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1622e41f4b71Sopenharmony_ci}) 1623e41f4b71Sopenharmony_ci``` 1624e41f4b71Sopenharmony_ci 1625e41f4b71Sopenharmony_ci 1626e41f4b71Sopenharmony_ci### once<sup>(deprecated)</sup> 1627e41f4b71Sopenharmony_ci 1628e41f4b71Sopenharmony_cionce(type: string, listener: EventListener): void 1629e41f4b71Sopenharmony_ci 1630e41f4b71Sopenharmony_ciAdds an event listener for the worker thread and removes the event listener after it is invoked once. 1631e41f4b71Sopenharmony_ci 1632e41f4b71Sopenharmony_ci> **NOTE**<br> 1633e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.once<sup>9+</sup>](#once9) instead. 1634e41f4b71Sopenharmony_ci 1635e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1636e41f4b71Sopenharmony_ci 1637e41f4b71Sopenharmony_ci**Parameters** 1638e41f4b71Sopenharmony_ci 1639e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1640e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ---------------- | 1641e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for.| 1642e41f4b71Sopenharmony_ci| listener | [EventListener](#eventlistenerdeprecated) | Yes | Callback to invoke when an event of the specified type occurs. | 1643e41f4b71Sopenharmony_ci 1644e41f4b71Sopenharmony_ci**Example** 1645e41f4b71Sopenharmony_ci 1646e41f4b71Sopenharmony_ci```ts 1647e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1648e41f4b71Sopenharmony_ciworkerInstance.once("alert", (e)=>{ 1649e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1650e41f4b71Sopenharmony_ci}) 1651e41f4b71Sopenharmony_ci``` 1652e41f4b71Sopenharmony_ci 1653e41f4b71Sopenharmony_ci 1654e41f4b71Sopenharmony_ci### off<sup>(deprecated)</sup> 1655e41f4b71Sopenharmony_ci 1656e41f4b71Sopenharmony_cioff(type: string, listener?: EventListener): void 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_ciRemoves an event listener for the worker thread. This API provides the same functionality as [removeEventListener<sup>(deprecated)</sup>](#removeeventlistenerdeprecated). 1659e41f4b71Sopenharmony_ci 1660e41f4b71Sopenharmony_ci> **NOTE**<br> 1661e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.off<sup>9+</sup>](#off9) instead. 1662e41f4b71Sopenharmony_ci 1663e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1664e41f4b71Sopenharmony_ci 1665e41f4b71Sopenharmony_ci**Parameters** 1666e41f4b71Sopenharmony_ci 1667e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1668e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | -------------------- | 1669e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event for which the event listener is to be removed.| 1670e41f4b71Sopenharmony_ci| listener | [EventListener](#eventlistenerdeprecated) | No | Callback to invoke when the listener is removed.| 1671e41f4b71Sopenharmony_ci 1672e41f4b71Sopenharmony_ci**Example** 1673e41f4b71Sopenharmony_ci 1674e41f4b71Sopenharmony_ci```ts 1675e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1676e41f4b71Sopenharmony_ci// Use on, once, or addEventListener to add a listener for the "alert" event, and use off to remove the listener. 1677e41f4b71Sopenharmony_ciworkerInstance.off("alert"); 1678e41f4b71Sopenharmony_ci``` 1679e41f4b71Sopenharmony_ci 1680e41f4b71Sopenharmony_ci 1681e41f4b71Sopenharmony_ci### terminate<sup>(deprecated)</sup> 1682e41f4b71Sopenharmony_ci 1683e41f4b71Sopenharmony_citerminate(): void 1684e41f4b71Sopenharmony_ci 1685e41f4b71Sopenharmony_ciTerminates the worker thread to stop it from receiving messages. 1686e41f4b71Sopenharmony_ci 1687e41f4b71Sopenharmony_ci> **NOTE**<br> 1688e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.terminate<sup>9+</sup>](#terminate9) instead. 1689e41f4b71Sopenharmony_ci 1690e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1691e41f4b71Sopenharmony_ci 1692e41f4b71Sopenharmony_ci**Example** 1693e41f4b71Sopenharmony_ci 1694e41f4b71Sopenharmony_ci```ts 1695e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1696e41f4b71Sopenharmony_ciworkerInstance.terminate(); 1697e41f4b71Sopenharmony_ci``` 1698e41f4b71Sopenharmony_ci 1699e41f4b71Sopenharmony_ci 1700e41f4b71Sopenharmony_ci### onexit<sup>(deprecated)</sup> 1701e41f4b71Sopenharmony_ci 1702e41f4b71Sopenharmony_cionexit?: (code: number) => void 1703e41f4b71Sopenharmony_ci 1704e41f4b71Sopenharmony_ciCalled when the worker thread exits. The event handler is executed in the host thread. In the callback function, the **code** value is of the number type, where the value **1** indicates abnormal exit and **0** indicates normal exit. 1705e41f4b71Sopenharmony_ci 1706e41f4b71Sopenharmony_ci> **NOTE**<br> 1707e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onexit<sup>9+</sup>](#onexit9) instead. 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1710e41f4b71Sopenharmony_ci 1711e41f4b71Sopenharmony_ci**Example** 1712e41f4b71Sopenharmony_ci 1713e41f4b71Sopenharmony_ci```ts 1714e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1715e41f4b71Sopenharmony_ciworkerInstance.onexit = (code) => { 1716e41f4b71Sopenharmony_ci console.log("onexit"); 1717e41f4b71Sopenharmony_ci} 1718e41f4b71Sopenharmony_ci 1719e41f4b71Sopenharmony_ci// onexit is executed in either of the following ways: 1720e41f4b71Sopenharmony_ci// Main thread 1721e41f4b71Sopenharmony_ciworkerInstance.terminate(); 1722e41f4b71Sopenharmony_ci 1723e41f4b71Sopenharmony_ci// Worker thread 1724e41f4b71Sopenharmony_ci//parentPort.close() 1725e41f4b71Sopenharmony_ci``` 1726e41f4b71Sopenharmony_ci 1727e41f4b71Sopenharmony_ci 1728e41f4b71Sopenharmony_ci### onerror<sup>(deprecated)</sup> 1729e41f4b71Sopenharmony_ci 1730e41f4b71Sopenharmony_cionerror?: (err: ErrorEvent) => void 1731e41f4b71Sopenharmony_ci 1732e41f4b71Sopenharmony_ciCalled when an exception occurs during worker execution. The event handler is executed in the host thread. In the callback function, the **err** type is [ErrorEvent](#errorevent), indicating the received abnormal data. 1733e41f4b71Sopenharmony_ci 1734e41f4b71Sopenharmony_ci> **NOTE**<br> 1735e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onerror<sup>9+</sup>](#onerror9) instead. 1736e41f4b71Sopenharmony_ci 1737e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1738e41f4b71Sopenharmony_ci 1739e41f4b71Sopenharmony_ci**Example** 1740e41f4b71Sopenharmony_ci 1741e41f4b71Sopenharmony_ci```ts 1742e41f4b71Sopenharmony_ciimport { worker, ErrorEvent } from '@kit.ArkTS'; 1743e41f4b71Sopenharmony_ci 1744e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1745e41f4b71Sopenharmony_ciworkerInstance.onerror = (err: ErrorEvent) => { 1746e41f4b71Sopenharmony_ci console.log("onerror" + err.message); 1747e41f4b71Sopenharmony_ci} 1748e41f4b71Sopenharmony_ci``` 1749e41f4b71Sopenharmony_ci 1750e41f4b71Sopenharmony_ci 1751e41f4b71Sopenharmony_ci### onmessage<sup>(deprecated)</sup> 1752e41f4b71Sopenharmony_ci 1753e41f4b71Sopenharmony_cionmessage?: (event: MessageEvent) => void 1754e41f4b71Sopenharmony_ci 1755e41f4b71Sopenharmony_ciCalled when the host thread receives a message sent by the worker thread through **workerPort.postMessage**. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvent](#messageeventt), indicating the received message data. 1756e41f4b71Sopenharmony_ci 1757e41f4b71Sopenharmony_ci> **NOTE**<br> 1758e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onmessage<sup>9+</sup>](#onmessage9) instead. 1759e41f4b71Sopenharmony_ci 1760e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1761e41f4b71Sopenharmony_ci 1762e41f4b71Sopenharmony_ci**Example** 1763e41f4b71Sopenharmony_ci 1764e41f4b71Sopenharmony_ci```ts 1765e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1766e41f4b71Sopenharmony_ci 1767e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1768e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e): void => { 1769e41f4b71Sopenharmony_ci console.log("onmessage"); 1770e41f4b71Sopenharmony_ci} 1771e41f4b71Sopenharmony_ci``` 1772e41f4b71Sopenharmony_ci 1773e41f4b71Sopenharmony_ci 1774e41f4b71Sopenharmony_ci### onmessageerror<sup>(deprecated)</sup> 1775e41f4b71Sopenharmony_ci 1776e41f4b71Sopenharmony_cionmessageerror?: (event: MessageEvent) => void 1777e41f4b71Sopenharmony_ci 1778e41f4b71Sopenharmony_ciCalled when the worker thread receives a message that cannot be serialized. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvent](#messageeventt), indicating the received message data. 1779e41f4b71Sopenharmony_ci 1780e41f4b71Sopenharmony_ci> **NOTE**<br> 1781e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onmessageerror<sup>9+</sup>](#onmessageerror9) instead. 1782e41f4b71Sopenharmony_ci 1783e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1784e41f4b71Sopenharmony_ci 1785e41f4b71Sopenharmony_ci**Example** 1786e41f4b71Sopenharmony_ci 1787e41f4b71Sopenharmony_ci```ts 1788e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 1789e41f4b71Sopenharmony_ci 1790e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1791e41f4b71Sopenharmony_ciworkerInstance.onmessageerror = (err) => { 1792e41f4b71Sopenharmony_ci console.log("onmessageerror"); 1793e41f4b71Sopenharmony_ci} 1794e41f4b71Sopenharmony_ci``` 1795e41f4b71Sopenharmony_ci 1796e41f4b71Sopenharmony_ci 1797e41f4b71Sopenharmony_ci## EventTarget<sup>(deprecated)</sup> 1798e41f4b71Sopenharmony_ci> **NOTE**<br> 1799e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [WorkerEventTarget<sup>9+</sup>](#workereventtarget9) instead. 1800e41f4b71Sopenharmony_ci 1801e41f4b71Sopenharmony_ci### addEventListener<sup>(deprecated)</sup> 1802e41f4b71Sopenharmony_ci 1803e41f4b71Sopenharmony_ciaddEventListener(type: string, listener: EventListener): void 1804e41f4b71Sopenharmony_ci 1805e41f4b71Sopenharmony_ciAdds an event listener for the worker thread. This API provides the same functionality as [on<sup>(deprecated)</sup>](#ondeprecated). 1806e41f4b71Sopenharmony_ci 1807e41f4b71Sopenharmony_ci> **NOTE**<br> 1808e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [addEventListener<sup>9+</sup>](#addeventlistener9) instead. 1809e41f4b71Sopenharmony_ci 1810e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1811e41f4b71Sopenharmony_ci 1812e41f4b71Sopenharmony_ci**Parameters** 1813e41f4b71Sopenharmony_ci 1814e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1815e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ---------------- | 1816e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event to listen for.| 1817e41f4b71Sopenharmony_ci| listener | [EventListener](#eventlistenerdeprecated) | Yes | Callback to invoke when an event of the specified type occurs. | 1818e41f4b71Sopenharmony_ci 1819e41f4b71Sopenharmony_ci**Example** 1820e41f4b71Sopenharmony_ci 1821e41f4b71Sopenharmony_ci```ts 1822e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1823e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 1824e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1825e41f4b71Sopenharmony_ci}) 1826e41f4b71Sopenharmony_ci``` 1827e41f4b71Sopenharmony_ci 1828e41f4b71Sopenharmony_ci 1829e41f4b71Sopenharmony_ci### removeEventListener<sup>(deprecated)</sup> 1830e41f4b71Sopenharmony_ci 1831e41f4b71Sopenharmony_ciremoveEventListener(type: string, callback?: EventListener): void 1832e41f4b71Sopenharmony_ci 1833e41f4b71Sopenharmony_ciRemoves an event listener for the worker thread. This API provides the same functionality as [off<sup>(deprecated)</sup>](#offdeprecated). 1834e41f4b71Sopenharmony_ci 1835e41f4b71Sopenharmony_ci> **NOTE**<br> 1836e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [removeEventListener<sup>9+</sup>](#removeeventlistener9) instead. 1837e41f4b71Sopenharmony_ci 1838e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1839e41f4b71Sopenharmony_ci 1840e41f4b71Sopenharmony_ci**Parameters** 1841e41f4b71Sopenharmony_ci 1842e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1843e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ------------------------ | 1844e41f4b71Sopenharmony_ci| type | string | Yes | Type of the event for which the event listener is to be removed.| 1845e41f4b71Sopenharmony_ci| callback | [EventListener](#eventlistenerdeprecated) | No | Callback to invoke when the listener is removed.| 1846e41f4b71Sopenharmony_ci 1847e41f4b71Sopenharmony_ci**Example** 1848e41f4b71Sopenharmony_ci 1849e41f4b71Sopenharmony_ci```ts 1850e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1851e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 1852e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1853e41f4b71Sopenharmony_ci}) 1854e41f4b71Sopenharmony_ciworkerInstance.removeEventListener("alert"); 1855e41f4b71Sopenharmony_ci``` 1856e41f4b71Sopenharmony_ci 1857e41f4b71Sopenharmony_ci 1858e41f4b71Sopenharmony_ci### dispatchEvent<sup>(deprecated)</sup> 1859e41f4b71Sopenharmony_ci 1860e41f4b71Sopenharmony_cidispatchEvent(event: Event): boolean 1861e41f4b71Sopenharmony_ci 1862e41f4b71Sopenharmony_ciDispatches the event defined for the worker thread. 1863e41f4b71Sopenharmony_ci 1864e41f4b71Sopenharmony_ci> **NOTE**<br> 1865e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [dispatchEvent<sup>9+</sup>](#dispatchevent9) instead. 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1868e41f4b71Sopenharmony_ci 1869e41f4b71Sopenharmony_ci**Parameters** 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1872e41f4b71Sopenharmony_ci| ------ | --------------- | ---- | ---------------- | 1873e41f4b71Sopenharmony_ci| event | [Event](#event) | Yes | Event to dispatch.| 1874e41f4b71Sopenharmony_ci 1875e41f4b71Sopenharmony_ci**Return value** 1876e41f4b71Sopenharmony_ci 1877e41f4b71Sopenharmony_ci| Type | Description | 1878e41f4b71Sopenharmony_ci| ------- | ------------------------------- | 1879e41f4b71Sopenharmony_ci| boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.| 1880e41f4b71Sopenharmony_ci 1881e41f4b71Sopenharmony_ci**Example** 1882e41f4b71Sopenharmony_ci 1883e41f4b71Sopenharmony_ci```ts 1884e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1885e41f4b71Sopenharmony_ci 1886e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet. 1887e41f4b71Sopenharmony_ci``` 1888e41f4b71Sopenharmony_ci 1889e41f4b71Sopenharmony_ciThe **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows: 1890e41f4b71Sopenharmony_ci 1891e41f4b71Sopenharmony_ci```ts 1892e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1893e41f4b71Sopenharmony_ci 1894e41f4b71Sopenharmony_ci// Usage 1: 1895e41f4b71Sopenharmony_ciworkerInstance.on("alert_on", (e)=>{ 1896e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1897e41f4b71Sopenharmony_ci}) 1898e41f4b71Sopenharmony_ciworkerInstance.once("alert_once", (e)=>{ 1899e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1900e41f4b71Sopenharmony_ci}) 1901e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert_add", (e)=>{ 1902e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1903e41f4b71Sopenharmony_ci}) 1904e41f4b71Sopenharmony_ci 1905e41f4b71Sopenharmony_ci// The event listener created by once is removed after being executed once. 1906e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is not supported yet. 1907e41f4b71Sopenharmony_ci// The event listener created by on will not be proactively deleted. 1908e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); 1909e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); 1910e41f4b71Sopenharmony_ci// The event listener created by addEventListener will not be proactively deleted. 1911e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); 1912e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); 1913e41f4b71Sopenharmony_ci 1914e41f4b71Sopenharmony_ci// Usage 2: 1915e41f4b71Sopenharmony_ci// The event type can be customized, and the special types "message", "messageerror", and "error" exist. 1916e41f4b71Sopenharmony_ci// When type = "message", the event handler defined by onmessage will also be executed. 1917e41f4b71Sopenharmony_ci// When type = "messageerror", the event handler defined by onmessageerror will also be executed. 1918e41f4b71Sopenharmony_ci// When type = "error", the event handler defined by onerror will also be executed. 1919e41f4b71Sopenharmony_ci// removeEventListener or off can be used to remove an event listener that is created by addEventListener, on, or once. 1920e41f4b71Sopenharmony_ci 1921e41f4b71Sopenharmony_ciworkerInstance.addEventListener("message", (e)=>{ 1922e41f4b71Sopenharmony_ci console.log("message listener callback"); 1923e41f4b71Sopenharmony_ci}) 1924e41f4b71Sopenharmony_ciworkerInstance.onmessage = function(e) { 1925e41f4b71Sopenharmony_ci console.log("onmessage : message listener callback"); 1926e41f4b71Sopenharmony_ci} 1927e41f4b71Sopenharmony_ci// When dispatchEvent is called to distribute the "message" event, the callback passed in addEventListener and onmessage will be invoked. 1928e41f4b71Sopenharmony_ciworkerInstance.dispatchEvent({type:"message", timeStamp:0}); 1929e41f4b71Sopenharmony_ci``` 1930e41f4b71Sopenharmony_ci### removeAllListener<sup>(deprecated)</sup> 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_ciremoveAllListener(): void 1933e41f4b71Sopenharmony_ci 1934e41f4b71Sopenharmony_ciRemoves all event listeners for the worker thread. 1935e41f4b71Sopenharmony_ci 1936e41f4b71Sopenharmony_ci> **NOTE**<br> 1937e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [removeAllListener<sup>9+</sup>](#removealllistener9) instead. 1938e41f4b71Sopenharmony_ci 1939e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1940e41f4b71Sopenharmony_ci 1941e41f4b71Sopenharmony_ci**Example** 1942e41f4b71Sopenharmony_ci 1943e41f4b71Sopenharmony_ci```ts 1944e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 1945e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 1946e41f4b71Sopenharmony_ci console.log("alert listener callback"); 1947e41f4b71Sopenharmony_ci}) 1948e41f4b71Sopenharmony_ciworkerInstance.removeAllListener(); 1949e41f4b71Sopenharmony_ci``` 1950e41f4b71Sopenharmony_ci 1951e41f4b71Sopenharmony_ci 1952e41f4b71Sopenharmony_ci## DedicatedWorkerGlobalScope<sup>(deprecated)</sup> 1953e41f4b71Sopenharmony_ci 1954e41f4b71Sopenharmony_ciImplements communication between the worker thread and the host thread. The **postMessage** API is used to send messages to the host thread, and the **close** API is used to terminate the worker thread. This class inherits from [WorkerGlobalScope](#workerglobalscopedeprecated). 1955e41f4b71Sopenharmony_ci 1956e41f4b71Sopenharmony_ci> **NOTE**<br> 1957e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>](#threadworkerglobalscope9) instead. 1958e41f4b71Sopenharmony_ci 1959e41f4b71Sopenharmony_ci### postMessage<sup>(deprecated)</sup> 1960e41f4b71Sopenharmony_ci 1961e41f4b71Sopenharmony_cipostMessage(messageObject: Object, transfer: Transferable[]): void 1962e41f4b71Sopenharmony_ci 1963e41f4b71Sopenharmony_ciSends a message from the worker thread to the host thread by transferring object ownership. 1964e41f4b71Sopenharmony_ci 1965e41f4b71Sopenharmony_ci> **NOTE**<br> 1966e41f4b71Sopenharmony_ci> This API is deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.postMessage<sup>9+</sup>](#postmessage9-2). 1967e41f4b71Sopenharmony_ci 1968e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1969e41f4b71Sopenharmony_ci 1970e41f4b71Sopenharmony_ci**Parameters** 1971e41f4b71Sopenharmony_ci 1972e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1973e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 1974e41f4b71Sopenharmony_ci| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 1975e41f4b71Sopenharmony_ci| transfer| Transferable[] | Yes | Currently, this parameter is not supported. | 1976e41f4b71Sopenharmony_ci 1977e41f4b71Sopenharmony_ci### postMessage<sup>9+</sup> 1978e41f4b71Sopenharmony_ci 1979e41f4b71Sopenharmony_cipostMessage(messageObject: Object, transfer: ArrayBuffer[]): void 1980e41f4b71Sopenharmony_ci 1981e41f4b71Sopenharmony_ciSends a message from the worker thread to the host thread by transferring object ownership. 1982e41f4b71Sopenharmony_ci 1983e41f4b71Sopenharmony_ci> **NOTE** 1984e41f4b71Sopenharmony_ci> 1985e41f4b71Sopenharmony_ci> The **DedicatedWorkerGlobalScope** class is deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.postMessage<sup>9+</sup>](#postmessage9-2). 1986e41f4b71Sopenharmony_ci 1987e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1988e41f4b71Sopenharmony_ci 1989e41f4b71Sopenharmony_ci**Parameters** 1990e41f4b71Sopenharmony_ci 1991e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1992e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------------------------------ | 1993e41f4b71Sopenharmony_ci| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 1994e41f4b71Sopenharmony_ci| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null.| 1995e41f4b71Sopenharmony_ci 1996e41f4b71Sopenharmony_ci**Example** 1997e41f4b71Sopenharmony_ci 1998e41f4b71Sopenharmony_ci```ts 1999e41f4b71Sopenharmony_ci// Main thread 2000e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2001e41f4b71Sopenharmony_ci 2002e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 2003e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 2004e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e): void => { 2005e41f4b71Sopenharmony_ci // let data = e.data; 2006e41f4b71Sopenharmony_ci console.log("receive data from worker.ets"); 2007e41f4b71Sopenharmony_ci} 2008e41f4b71Sopenharmony_ci``` 2009e41f4b71Sopenharmony_ci```ts 2010e41f4b71Sopenharmony_ci// worker.ets 2011e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2012e41f4b71Sopenharmony_ci 2013e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 2014e41f4b71Sopenharmony_ciworkerPort.onmessage = (e): void => { 2015e41f4b71Sopenharmony_ci // let data = e.data; 2016e41f4b71Sopenharmony_ci let buffer = new ArrayBuffer(5) 2017e41f4b71Sopenharmony_ci workerPort.postMessage(buffer, [buffer]); 2018e41f4b71Sopenharmony_ci} 2019e41f4b71Sopenharmony_ci``` 2020e41f4b71Sopenharmony_ci 2021e41f4b71Sopenharmony_ci### postMessage<sup>(deprecated)</sup> 2022e41f4b71Sopenharmony_ci 2023e41f4b71Sopenharmony_cipostMessage(messageObject: Object, options?: PostMessageOptions): void 2024e41f4b71Sopenharmony_ci 2025e41f4b71Sopenharmony_ciSends a message from the worker thread to the host thread by transferring object ownership or copying data. 2026e41f4b71Sopenharmony_ci 2027e41f4b71Sopenharmony_ci> **NOTE**<br> 2028e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.postMessage<sup>9+</sup>](#postmessage9-3). 2029e41f4b71Sopenharmony_ci 2030e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2031e41f4b71Sopenharmony_ci 2032e41f4b71Sopenharmony_ci**Parameters** 2033e41f4b71Sopenharmony_ci 2034e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2035e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2036e41f4b71Sopenharmony_ci| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| 2037e41f4b71Sopenharmony_ci| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the host thread and becomes unavailable in the worker thread. The objects are available only in the host thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.| 2038e41f4b71Sopenharmony_ci 2039e41f4b71Sopenharmony_ci**Example** 2040e41f4b71Sopenharmony_ci 2041e41f4b71Sopenharmony_ci<!--no_check--> 2042e41f4b71Sopenharmony_ci```ts 2043e41f4b71Sopenharmony_ci// Main thread 2044e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2045e41f4b71Sopenharmony_ci 2046e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("entry/ets/workers/worker.ets"); 2047e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 2048e41f4b71Sopenharmony_ciworkerInstance.onmessage = (): void => { 2049e41f4b71Sopenharmony_ci console.log("receive data from worker.ets"); 2050e41f4b71Sopenharmony_ci} 2051e41f4b71Sopenharmony_ci``` 2052e41f4b71Sopenharmony_ci```ts 2053e41f4b71Sopenharmony_ci// worker.ets 2054e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, worker } from '@kit.ArkTS'; 2055e41f4b71Sopenharmony_ci 2056e41f4b71Sopenharmony_ciconst parentPort = worker.parentPort; 2057e41f4b71Sopenharmony_ciparentPort.onmessage = (e: MessageEvents) => { 2058e41f4b71Sopenharmony_ci parentPort.postMessage("receive data from main thread"); 2059e41f4b71Sopenharmony_ci} 2060e41f4b71Sopenharmony_ci``` 2061e41f4b71Sopenharmony_ci 2062e41f4b71Sopenharmony_ci### close<sup>(deprecated)</sup> 2063e41f4b71Sopenharmony_ci 2064e41f4b71Sopenharmony_ciclose(): void 2065e41f4b71Sopenharmony_ci 2066e41f4b71Sopenharmony_ciTerminates the worker thread to stop it from receiving messages. 2067e41f4b71Sopenharmony_ci 2068e41f4b71Sopenharmony_ci> **NOTE**<br> 2069e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.close<sup>9+</sup>](#close9). 2070e41f4b71Sopenharmony_ci 2071e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2072e41f4b71Sopenharmony_ci 2073e41f4b71Sopenharmony_ci**Example** 2074e41f4b71Sopenharmony_ci 2075e41f4b71Sopenharmony_ci```ts 2076e41f4b71Sopenharmony_ci// Main thread 2077e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2078e41f4b71Sopenharmony_ci 2079e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 2080e41f4b71Sopenharmony_ci``` 2081e41f4b71Sopenharmony_ci```ts 2082e41f4b71Sopenharmony_ci// worker.ets 2083e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2084e41f4b71Sopenharmony_ci 2085e41f4b71Sopenharmony_ciconst parentPort = worker.parentPort; 2086e41f4b71Sopenharmony_ciparentPort.onmessage = (e): void => { 2087e41f4b71Sopenharmony_ci parentPort.close() 2088e41f4b71Sopenharmony_ci} 2089e41f4b71Sopenharmony_ci``` 2090e41f4b71Sopenharmony_ci 2091e41f4b71Sopenharmony_ci 2092e41f4b71Sopenharmony_ci### onmessage<sup>(deprecated)</sup> 2093e41f4b71Sopenharmony_ci 2094e41f4b71Sopenharmony_cionmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void 2095e41f4b71Sopenharmony_ci 2096e41f4b71Sopenharmony_ciCalled when the worker thread receives a message sent by the host thread through **postMessage**. The event handler is executed in the worker thread. In the callback function, **this** indicates the caller's [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscopedeprecated), and the **ev** type is [MessageEvent](#messageeventt), indicating the received message data. 2097e41f4b71Sopenharmony_ci 2098e41f4b71Sopenharmony_ci> **NOTE**<br> 2099e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.onmessage<sup>9+</sup>](#onmessage9-1). 2100e41f4b71Sopenharmony_ci 2101e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2102e41f4b71Sopenharmony_ci 2103e41f4b71Sopenharmony_ci**Example** 2104e41f4b71Sopenharmony_ci 2105e41f4b71Sopenharmony_ci```ts 2106e41f4b71Sopenharmony_ci// Main thread 2107e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2108e41f4b71Sopenharmony_ci 2109e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 2110e41f4b71Sopenharmony_ciworkerInstance.postMessage("hello world"); 2111e41f4b71Sopenharmony_ci``` 2112e41f4b71Sopenharmony_ci```ts 2113e41f4b71Sopenharmony_ci// worker.ets 2114e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2115e41f4b71Sopenharmony_ci 2116e41f4b71Sopenharmony_ciconst parentPort = worker.parentPort; 2117e41f4b71Sopenharmony_ciparentPort.onmessage = (e): void => { 2118e41f4b71Sopenharmony_ci console.log("receive main thread message"); 2119e41f4b71Sopenharmony_ci} 2120e41f4b71Sopenharmony_ci``` 2121e41f4b71Sopenharmony_ci 2122e41f4b71Sopenharmony_ci 2123e41f4b71Sopenharmony_ci### onmessageerror<sup>(deprecated)</sup> 2124e41f4b71Sopenharmony_ci 2125e41f4b71Sopenharmony_cionmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void 2126e41f4b71Sopenharmony_ci 2127e41f4b71Sopenharmony_ciCalled when the worker thread receives a message that cannot be deserialized. The event handler is executed in the worker thread. In the callback function, **this** indicates the caller's [DedicatedWorkerGlobalScope](#threadworkerglobalscope9), and the **ev** type is [MessageEvent](#dedicatedworkerglobalscopedeprecated), indicating the received message data. 2128e41f4b71Sopenharmony_ci 2129e41f4b71Sopenharmony_ci> **NOTE**<br> 2130e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.onmessageerror<sup>9+</sup>](#onmessageerror9-1). 2131e41f4b71Sopenharmony_ci 2132e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2133e41f4b71Sopenharmony_ci 2134e41f4b71Sopenharmony_ci**Example** 2135e41f4b71Sopenharmony_ci 2136e41f4b71Sopenharmony_ci```ts 2137e41f4b71Sopenharmony_ci// Main thread 2138e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2139e41f4b71Sopenharmony_ci 2140e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 2141e41f4b71Sopenharmony_ci``` 2142e41f4b71Sopenharmony_ci```ts 2143e41f4b71Sopenharmony_ci// worker.ets 2144e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2145e41f4b71Sopenharmony_ci 2146e41f4b71Sopenharmony_ciconst parentPort = worker.parentPort; 2147e41f4b71Sopenharmony_ciparentPort.onmessageerror = (e) => { 2148e41f4b71Sopenharmony_ci console.log("worker.ets onmessageerror") 2149e41f4b71Sopenharmony_ci} 2150e41f4b71Sopenharmony_ci``` 2151e41f4b71Sopenharmony_ci 2152e41f4b71Sopenharmony_ci 2153e41f4b71Sopenharmony_ci## PostMessageOptions 2154e41f4b71Sopenharmony_ci 2155e41f4b71Sopenharmony_ciDefines the object for which the ownership is to be transferred during data transfer. The object must be an **ArrayBuffer** instance. After the ownership is transferred, the object becomes unavailable in the sender and can be used only in the receiver. 2156e41f4b71Sopenharmony_ci 2157e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2158e41f4b71Sopenharmony_ci 2159e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2160e41f4b71Sopenharmony_ci 2161e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 2162e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ---- | --------------------------------- | 2163e41f4b71Sopenharmony_ci| transfer | Object[] | Yes | Yes | **ArrayBuffer** array used to transfer the ownership. The array cannot be **null**.| 2164e41f4b71Sopenharmony_ci 2165e41f4b71Sopenharmony_ci 2166e41f4b71Sopenharmony_ci## Event 2167e41f4b71Sopenharmony_ci 2168e41f4b71Sopenharmony_ciDefines the event. 2169e41f4b71Sopenharmony_ci 2170e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2171e41f4b71Sopenharmony_ci 2172e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2173e41f4b71Sopenharmony_ci 2174e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 2175e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---- | -------------------------------------------- | 2176e41f4b71Sopenharmony_ci| type | string | Yes | No | Type of the event. | 2177e41f4b71Sopenharmony_ci| timeStamp | number | Yes | No | Timestamp (accurate to millisecond) when the event is created. This parameter is not supported yet.| 2178e41f4b71Sopenharmony_ci 2179e41f4b71Sopenharmony_ci 2180e41f4b71Sopenharmony_ci## EventListener<sup>(deprecated)</sup> 2181e41f4b71Sopenharmony_ci 2182e41f4b71Sopenharmony_ciImplements event listening. 2183e41f4b71Sopenharmony_ci 2184e41f4b71Sopenharmony_ci> **NOTE**<br> 2185e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [WorkerEventListener<sup>9+</sup>](#workereventlistener9) instead. 2186e41f4b71Sopenharmony_ci 2187e41f4b71Sopenharmony_ci### (evt: Event)<sup>(deprecated)</sup> 2188e41f4b71Sopenharmony_ci 2189e41f4b71Sopenharmony_ci(evt: Event): void | Promise<void> 2190e41f4b71Sopenharmony_ci 2191e41f4b71Sopenharmony_ci> **NOTE** 2192e41f4b71Sopenharmony_ci> 2193e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [(event:Event)<sup>9+</sup>](#event-event9) instead. 2194e41f4b71Sopenharmony_ci 2195e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2196e41f4b71Sopenharmony_ci 2197e41f4b71Sopenharmony_ci**Parameters** 2198e41f4b71Sopenharmony_ci 2199e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2200e41f4b71Sopenharmony_ci| ------ | --------------- | ---- | -------------- | 2201e41f4b71Sopenharmony_ci| evt | [Event](#event) | Yes | Event class for the callback to invoke.| 2202e41f4b71Sopenharmony_ci 2203e41f4b71Sopenharmony_ci**Return value** 2204e41f4b71Sopenharmony_ci 2205e41f4b71Sopenharmony_ci| Type | Description | 2206e41f4b71Sopenharmony_ci| ------------------------------------- | ------------------------------- | 2207e41f4b71Sopenharmony_ci| void \| Promise<void> | Returns no value or returns a **Promise**.| 2208e41f4b71Sopenharmony_ci 2209e41f4b71Sopenharmony_ci**Example** 2210e41f4b71Sopenharmony_ci 2211e41f4b71Sopenharmony_ci```ts 2212e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets"); 2213e41f4b71Sopenharmony_ciworkerInstance.addEventListener("alert", (e)=>{ 2214e41f4b71Sopenharmony_ci console.log("alert listener callback"); 2215e41f4b71Sopenharmony_ci}) 2216e41f4b71Sopenharmony_ci``` 2217e41f4b71Sopenharmony_ci 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_ci## ErrorEvent 2220e41f4b71Sopenharmony_ci 2221e41f4b71Sopenharmony_ciProvides detailed information about the exception that occurs during worker execution. The **ErrorEvent** class inherits from [Event](#event). 2222e41f4b71Sopenharmony_ci 2223e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2226e41f4b71Sopenharmony_ci 2227e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 2228e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ---- | -------------------- | 2229e41f4b71Sopenharmony_ci| message | string | Yes | No | Information about the exception.| 2230e41f4b71Sopenharmony_ci| filename | string | Yes | No | File where the exception is located.| 2231e41f4b71Sopenharmony_ci| lineno | number | Yes | No | Serial number of the line where the exception is located. | 2232e41f4b71Sopenharmony_ci| colno | number | Yes | No | Serial number of the column where the exception is located. | 2233e41f4b71Sopenharmony_ci| error | Object | Yes | No | Type of the exception. | 2234e41f4b71Sopenharmony_ci 2235e41f4b71Sopenharmony_ci 2236e41f4b71Sopenharmony_ci## MessageEvent\<T\> 2237e41f4b71Sopenharmony_ci 2238e41f4b71Sopenharmony_ciHolds the data transferred between worker threads. 2239e41f4b71Sopenharmony_ci 2240e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2241e41f4b71Sopenharmony_ci 2242e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2243e41f4b71Sopenharmony_ci 2244e41f4b71Sopenharmony_ci| Name| Type| Readable| Writable| Description | 2245e41f4b71Sopenharmony_ci| ---- | ---- | ---- | ---- | ------------------ | 2246e41f4b71Sopenharmony_ci| data | T | Yes | No | Data transferred between threads.| 2247e41f4b71Sopenharmony_ci 2248e41f4b71Sopenharmony_ci 2249e41f4b71Sopenharmony_ci## WorkerGlobalScope<sup>(deprecated)</sup> 2250e41f4b71Sopenharmony_ci 2251e41f4b71Sopenharmony_ciImplements the running environment of the worker thread. The **WorkerGlobalScope** class inherits from [EventTarget](#eventtargetdeprecated). 2252e41f4b71Sopenharmony_ci 2253e41f4b71Sopenharmony_ci> **NOTE**<br> 2254e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [GlobalScope<sup>9+</sup>](#globalscope9) instead. 2255e41f4b71Sopenharmony_ci 2256e41f4b71Sopenharmony_ci### Attributes 2257e41f4b71Sopenharmony_ci 2258e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2259e41f4b71Sopenharmony_ci 2260e41f4b71Sopenharmony_ci| Name| Type | Readable| Writable| Description | 2261e41f4b71Sopenharmony_ci| ---- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------- | 2262e41f4b71Sopenharmony_ci| name | string | Yes | No | **Worker** instance specified when there is a new **Worker** instance.| 2263e41f4b71Sopenharmony_ci| self | [WorkerGlobalScope](#workerglobalscopedeprecated) & typeof globalThis | Yes | No | **WorkerGlobalScope**. | 2264e41f4b71Sopenharmony_ci 2265e41f4b71Sopenharmony_ci 2266e41f4b71Sopenharmony_ci### onerror<sup>(deprecated)</sup> 2267e41f4b71Sopenharmony_ci 2268e41f4b71Sopenharmony_cionerror?: (ev: ErrorEvent) => void 2269e41f4b71Sopenharmony_ci 2270e41f4b71Sopenharmony_ciCalled when an exception occurs during worker execution. The event handler is executed in the worker thread. In the callback function, the **ev** type is [ErrorEvent](#errorevent), indicating the received abnormal data. 2271e41f4b71Sopenharmony_ci 2272e41f4b71Sopenharmony_ci> **NOTE**<br> 2273e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [GlobalScope<sup>9+</sup>.onerror<sup>9+</sup>](#onerror9-1). 2274e41f4b71Sopenharmony_ci 2275e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2276e41f4b71Sopenharmony_ci 2277e41f4b71Sopenharmony_ci**Example** 2278e41f4b71Sopenharmony_ci 2279e41f4b71Sopenharmony_ci```ts 2280e41f4b71Sopenharmony_ci// Main thread 2281e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 2282e41f4b71Sopenharmony_ci 2283e41f4b71Sopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.ets") 2284e41f4b71Sopenharmony_ci``` 2285e41f4b71Sopenharmony_ci```ts 2286e41f4b71Sopenharmony_ci// worker.ets 2287e41f4b71Sopenharmony_ciimport { worker, ErrorEvent } from '@kit.ArkTS'; 2288e41f4b71Sopenharmony_ci 2289e41f4b71Sopenharmony_ciconst parentPort = worker.parentPort 2290e41f4b71Sopenharmony_ciparentPort.onerror = (err: ErrorEvent) => { 2291e41f4b71Sopenharmony_ci console.log("worker.ets onerror" + err.message) 2292e41f4b71Sopenharmony_ci} 2293e41f4b71Sopenharmony_ci``` 2294e41f4b71Sopenharmony_ci 2295e41f4b71Sopenharmony_ci 2296e41f4b71Sopenharmony_ci## More Information 2297e41f4b71Sopenharmony_ci 2298e41f4b71Sopenharmony_ci### Sequenceable Data Types 2299e41f4b71Sopenharmony_ci 2300e41f4b71Sopenharmony_ciThe following object types are supported: basic types except Symbol, Date, String, RegExp, Array, Map, Set, Object (simple objects only, for example, objects created using **{}** or **new Object**), ArrayBuffer, and typedArray. (Note that only attributes can be transferred for common objects. Prototypes and methods cannot be transferred.) 2301e41f4b71Sopenharmony_ci 2302e41f4b71Sopenharmony_ciException: When an object created through a custom class is passed, no serialization error occurs. However, the attributes (such as Function) of the custom class cannot be passed through serialization. 2303e41f4b71Sopenharmony_ci> **NOTE**<br> 2304e41f4b71Sopenharmony_ci> An FA project of API version 9 is used as an example. 2305e41f4b71Sopenharmony_ci 2306e41f4b71Sopenharmony_ci```ts 2307e41f4b71Sopenharmony_ci// Main thread 2308e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 2309e41f4b71Sopenharmony_ci 2310e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("workers/worker.ets"); 2311e41f4b71Sopenharmony_ciworkerInstance.postMessage("message from main thread to worker"); 2312e41f4b71Sopenharmony_ciworkerInstance.onmessage = (d: MessageEvents): void => { 2313e41f4b71Sopenharmony_ci // When the worker thread passes obj2, data contains obj2, excluding the Init or SetName method. 2314e41f4b71Sopenharmony_ci let data: string = d.data; 2315e41f4b71Sopenharmony_ci} 2316e41f4b71Sopenharmony_ci``` 2317e41f4b71Sopenharmony_ci```ts 2318e41f4b71Sopenharmony_ci// worker.ets 2319e41f4b71Sopenharmony_ciimport { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 2320e41f4b71Sopenharmony_ci 2321e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 2322e41f4b71Sopenharmony_ciclass MyModel { 2323e41f4b71Sopenharmony_ci name = "undefined" 2324e41f4b71Sopenharmony_ci Init() { 2325e41f4b71Sopenharmony_ci this.name = "MyModel" 2326e41f4b71Sopenharmony_ci } 2327e41f4b71Sopenharmony_ci} 2328e41f4b71Sopenharmony_ciworkerPort.onmessage = (d: MessageEvents): void => { 2329e41f4b71Sopenharmony_ci console.log("worker.ets onmessage"); 2330e41f4b71Sopenharmony_ci let data: string = d.data; 2331e41f4b71Sopenharmony_ci let func1 = () => { 2332e41f4b71Sopenharmony_ci console.log("post message is function"); 2333e41f4b71Sopenharmony_ci } 2334e41f4b71Sopenharmony_ci // workerPort.postMessage(func1); A serialization error occurs when passing func1. 2335e41f4b71Sopenharmony_ci // let obj1: obj | null = null; 2336e41f4b71Sopenharmony_ci // if (obj1) { 2337e41f4b71Sopenharmony_ci // workerPort.postMessage(obj1 as obj); // A serialization error occurs when passing obj1. 2338e41f4b71Sopenharmony_ci // } 2339e41f4b71Sopenharmony_ci let obj2 = new MyModel(); 2340e41f4b71Sopenharmony_ci workerPort.postMessage(obj2); // No serialization error occurs when passing obj2. 2341e41f4b71Sopenharmony_ci} 2342e41f4b71Sopenharmony_ciworkerPort.onmessageerror = () => { 2343e41f4b71Sopenharmony_ci console.log("worker.ets onmessageerror"); 2344e41f4b71Sopenharmony_ci} 2345e41f4b71Sopenharmony_ciworkerPort.onerror = (err: ErrorEvent) => { 2346e41f4b71Sopenharmony_ci console.log("worker.ets onerror" + err.message); 2347e41f4b71Sopenharmony_ci} 2348e41f4b71Sopenharmony_ci``` 2349e41f4b71Sopenharmony_ci 2350e41f4b71Sopenharmony_ci### Memory Model 2351e41f4b71Sopenharmony_ciThe worker thread is implemented based on the actor model. In the worker interaction process, the JS main thread can create multiple worker threads, each of which are isolated and transfer data through serialization. They complete computing tasks and return the result to the main thread. 2352e41f4b71Sopenharmony_ci 2353e41f4b71Sopenharmony_ciEach actor concurrently processes tasks of the main thread. For each actor, there is a message queue and a single-thread execution module. The message queue receives requests from the main thread and other actors; the single-thread execution module serially processes requests, sends requests to other actors, and creates new actors. These isolated actors use the asynchronous mode and can run concurrently. 2354e41f4b71Sopenharmony_ci 2355e41f4b71Sopenharmony_ci## Sample Code 2356e41f4b71Sopenharmony_ci> **NOTE**<br> 2357e41f4b71Sopenharmony_ci> Two projects of API version 9 are used as an example.<br> Only the FA model is supported in API version 8 and earlier versions. If you want to use API version 8 or earlier, change the API for constructing the **Worker** instance and the API for creating an object in the worker thread for communicating with the main thread. 2358e41f4b71Sopenharmony_ci### FA Model 2359e41f4b71Sopenharmony_ci 2360e41f4b71Sopenharmony_ci```ts 2361e41f4b71Sopenharmony_ci// Main thread (The following assumes that the workers directory and pages directory are at the same level.) 2362e41f4b71Sopenharmony_ciimport { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 2363e41f4b71Sopenharmony_ci 2364e41f4b71Sopenharmony_ci// Create a Worker instance in the main thread. 2365e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("workers/worker.ets"); 2366e41f4b71Sopenharmony_ci 2367e41f4b71Sopenharmony_ci// The main thread transfers information to the worker thread. 2368e41f4b71Sopenharmony_ciworkerInstance.postMessage("123"); 2369e41f4b71Sopenharmony_ci 2370e41f4b71Sopenharmony_ci// The main thread receives information from the worker thread. 2371e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 2372e41f4b71Sopenharmony_ci // data carries the information sent by the worker thread. 2373e41f4b71Sopenharmony_ci let data: string = e.data; 2374e41f4b71Sopenharmony_ci console.log("main thread onmessage"); 2375e41f4b71Sopenharmony_ci 2376e41f4b71Sopenharmony_ci // Terminate the Worker instance. 2377e41f4b71Sopenharmony_ci workerInstance.terminate(); 2378e41f4b71Sopenharmony_ci} 2379e41f4b71Sopenharmony_ci 2380e41f4b71Sopenharmony_ci// Call onexit(). 2381e41f4b71Sopenharmony_ciworkerInstance.onexit = (code) => { 2382e41f4b71Sopenharmony_ci console.log("main thread terminate"); 2383e41f4b71Sopenharmony_ci} 2384e41f4b71Sopenharmony_ci 2385e41f4b71Sopenharmony_ciworkerInstance.onerror = (err: ErrorEvent) => { 2386e41f4b71Sopenharmony_ci console.log("main error message " + err.message); 2387e41f4b71Sopenharmony_ci} 2388e41f4b71Sopenharmony_ci``` 2389e41f4b71Sopenharmony_ci```ts 2390e41f4b71Sopenharmony_ci// worker.ets 2391e41f4b71Sopenharmony_ciimport { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 2392e41f4b71Sopenharmony_ci 2393e41f4b71Sopenharmony_ci// Create an object in the worker thread for communicating with the main thread. 2394e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort 2395e41f4b71Sopenharmony_ci 2396e41f4b71Sopenharmony_ci// The worker thread receives information from the main thread. 2397e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 2398e41f4b71Sopenharmony_ci // data carries the information sent by the main thread. 2399e41f4b71Sopenharmony_ci let data: string = e.data; 2400e41f4b71Sopenharmony_ci console.log("worker.ets onmessage"); 2401e41f4b71Sopenharmony_ci 2402e41f4b71Sopenharmony_ci // The worker thread sends information to the main thread. 2403e41f4b71Sopenharmony_ci workerPort.postMessage("123") 2404e41f4b71Sopenharmony_ci} 2405e41f4b71Sopenharmony_ci 2406e41f4b71Sopenharmony_ci// Trigger a callback when an error occurs in the worker thread. 2407e41f4b71Sopenharmony_ciworkerPort.onerror = (err: ErrorEvent) => { 2408e41f4b71Sopenharmony_ci console.log("worker.ets onerror"); 2409e41f4b71Sopenharmony_ci} 2410e41f4b71Sopenharmony_ci``` 2411e41f4b71Sopenharmony_ciConfiguration of the **build-profile.json5** file: 2412e41f4b71Sopenharmony_ci```json 2413e41f4b71Sopenharmony_ci "buildOption": { 2414e41f4b71Sopenharmony_ci "sourceOption": { 2415e41f4b71Sopenharmony_ci "workers": [ 2416e41f4b71Sopenharmony_ci "./src/main/ets/entryability/workers/worker.ets" 2417e41f4b71Sopenharmony_ci ] 2418e41f4b71Sopenharmony_ci } 2419e41f4b71Sopenharmony_ci } 2420e41f4b71Sopenharmony_ci``` 2421e41f4b71Sopenharmony_ci### Stage Model 2422e41f4b71Sopenharmony_ci```ts 2423e41f4b71Sopenharmony_ci// Main thread (The following assumes that the workers directory and pages directory are at different levels.) 2424e41f4b71Sopenharmony_ciimport { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 2425e41f4b71Sopenharmony_ci 2426e41f4b71Sopenharmony_ci// Create a Worker instance in the main thread. 2427e41f4b71Sopenharmony_ciconst workerInstance = new worker.ThreadWorker("entry/ets/pages/workers/worker.ets"); 2428e41f4b71Sopenharmony_ci 2429e41f4b71Sopenharmony_ci// The main thread transfers information to the worker thread. 2430e41f4b71Sopenharmony_ciworkerInstance.postMessage("123"); 2431e41f4b71Sopenharmony_ci 2432e41f4b71Sopenharmony_ci// The main thread receives information from the worker thread. 2433e41f4b71Sopenharmony_ciworkerInstance.onmessage = (e: MessageEvents): void => { 2434e41f4b71Sopenharmony_ci // data carries the information sent by the worker thread. 2435e41f4b71Sopenharmony_ci let data: string = e.data; 2436e41f4b71Sopenharmony_ci console.log("main thread onmessage"); 2437e41f4b71Sopenharmony_ci 2438e41f4b71Sopenharmony_ci // Terminate the Worker instance. 2439e41f4b71Sopenharmony_ci workerInstance.terminate(); 2440e41f4b71Sopenharmony_ci} 2441e41f4b71Sopenharmony_ci// Call onexit(). 2442e41f4b71Sopenharmony_ciworkerInstance.onexit = (code) => { 2443e41f4b71Sopenharmony_ci console.log("main thread terminate"); 2444e41f4b71Sopenharmony_ci} 2445e41f4b71Sopenharmony_ci 2446e41f4b71Sopenharmony_ciworkerInstance.onerror = (err: ErrorEvent) => { 2447e41f4b71Sopenharmony_ci console.log("main error message " + err.message); 2448e41f4b71Sopenharmony_ci} 2449e41f4b71Sopenharmony_ci``` 2450e41f4b71Sopenharmony_ci```ts 2451e41f4b71Sopenharmony_ci// worker.ets 2452e41f4b71Sopenharmony_ciimport { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS'; 2453e41f4b71Sopenharmony_ci 2454e41f4b71Sopenharmony_ci// Create an object in the worker thread for communicating with the main thread. 2455e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort 2456e41f4b71Sopenharmony_ci 2457e41f4b71Sopenharmony_ci// The worker thread receives information from the main thread. 2458e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents): void => { 2459e41f4b71Sopenharmony_ci // data carries the information sent by the main thread. 2460e41f4b71Sopenharmony_ci let data: string = e.data; 2461e41f4b71Sopenharmony_ci console.log("worker.ets onmessage"); 2462e41f4b71Sopenharmony_ci 2463e41f4b71Sopenharmony_ci // The worker thread sends information to the main thread. 2464e41f4b71Sopenharmony_ci workerPort.postMessage("123") 2465e41f4b71Sopenharmony_ci} 2466e41f4b71Sopenharmony_ci 2467e41f4b71Sopenharmony_ci// Trigger a callback when an error occurs in the worker thread. 2468e41f4b71Sopenharmony_ciworkerPort.onerror = (err: ErrorEvent) => { 2469e41f4b71Sopenharmony_ci console.log("worker.ets onerror" + err.message); 2470e41f4b71Sopenharmony_ci} 2471e41f4b71Sopenharmony_ci``` 2472e41f4b71Sopenharmony_ciConfiguration of the **build-profile.json5** file: 2473e41f4b71Sopenharmony_ci```json 2474e41f4b71Sopenharmony_ci "buildOption": { 2475e41f4b71Sopenharmony_ci "sourceOption": { 2476e41f4b71Sopenharmony_ci "workers": [ 2477e41f4b71Sopenharmony_ci "./src/main/ets/pages/workers/worker.ets" 2478e41f4b71Sopenharmony_ci ] 2479e41f4b71Sopenharmony_ci } 2480e41f4b71Sopenharmony_ci } 2481e41f4b71Sopenharmony_ci``` 2482e41f4b71Sopenharmony_ci<!--no_check--> 2483