harmony 鸿蒙Synchronous Calls to Host Thread Interfaces from Worker

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

Synchronous Calls to Host Thread Interfaces from Worker

If an interface is already implemented in the host thread and needs to be called by Worker, you can achieve this by using the approach described in this topic.

The following example demonstrates how Worker can synchronously call an interface implemented in the host thread.

  1. Implement the interface in the host thread and create a Worker object. Register the interface to be called on the Worker object.
   // IconItemSource.ets
   export class IconItemSource {
     image: string|Resource = '';
     text: string|Resource = '';
   
     constructor(image: string|Resource = '', text: string|Resource = '') {
       this.image = image;
       this.text = text;
     }
   }
   // WorkerCallGlobalUsage.ets
   import worker from '@ohos.worker';
   import { IconItemSource } from './IconItemSource';
   
   // Create a Worker object.
   const workerInstance: worker.ThreadWorker = new worker.ThreadWorker("entry/ets/pages/workers/Worker.ts");
   
   class PicData {
     public iconItemSourceList: IconItemSource[] = [];
   
     public setUp(): string {
       for (let index = 0; index < 20; index++) {
         const numStart: number = index * 6;
         // Use six images in the loop.
         this.iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
         this.iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
         this.iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
         this.iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
         this.iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
         this.iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
   
       }
       return "setUpIconItemSourceList success!";
     }
   }
   
   let picData = new PicData();
   // Register the object to be called on the Worker object.
   workerInstance.registerGlobalCallObject("picData", picData);
   workerInstance.postMessage("run setUp in picData");
  1. In Worker, use the callGlobalCallObjectMethod interface to call the setUp() method implemented in the host thread.
   // Worker.ets
   import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
   const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
   try {
     // Call the method without parameters.
     let res: string = workerPort.callGlobalCallObjectMethod("picData", "setUp", 0) as string;
     console.error("worker: ", res);
   } catch (error) {
     // Handle exceptions.
     console.error("worker: error code is " + error.code + " error message is " + error.message);
   }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Configuring arkOptions in build-profile.json5

harmony 鸿蒙Asynchronous Lock

harmony 鸿蒙Ark Bytecode File Format

harmony 鸿蒙Naming Conventions for Ark Bytecode Functions

harmony 鸿蒙Ark Bytecode Fundamentals

harmony 鸿蒙Overview of Ark Bytecode

harmony 鸿蒙Shared Container

harmony 鸿蒙Asynchronous Waiting

harmony 鸿蒙ArkTS Cross-Language Interaction

0  赞