• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Synchronous Calls to Host Thread Interfaces from Worker
2
3This topic describes the approach that Worker uses to call an interface that is already implemented in the host thread.
4
5The example below demonstrates how Worker can synchronously call an interface from the host thread.
6
71. Implement the interface in the host thread and create a Worker object. Register the interface to be called on the Worker object.
8
9   ```ts
10   // IconItemSource.ets
11   export class IconItemSource {
12     image: string | Resource = '';
13     text: string | Resource = '';
14
15     constructor(image: string | Resource = '', text: string | Resource = '') {
16       this.image = image;
17       this.text = text;
18     }
19   }
20   ```
21   <!-- @[implement_child_thread_task](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IconItemSource.ets) -->
22
23   ```ts
24   // WorkerCallGlobalUsage.ets
25   import worker from '@ohos.worker';
26   import { IconItemSource } from './IconItemSource';
27
28   // Create a Worker object.
29   const workerInstance: worker.ThreadWorker = new worker.ThreadWorker("entry/ets/pages/workers/Worker.ts");
30
31   class PicData {
32     public iconItemSourceList: IconItemSource[] = [];
33
34     public setUp(): string {
35       for (let index = 0; index < 20; index++) {
36         const numStart: number = index * 6;
37         // Use six images in the loop.
38         this.iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
39         this.iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
40         this.iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
41         this.iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
42         this.iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
43         this.iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
44
45       }
46       return "setUpIconItemSourceList success!";
47     }
48   }
49
50   let picData = new PicData();
51   // Register the object to be called on the Worker object.
52   workerInstance.registerGlobalCallObject("picData", picData);
53   workerInstance.postMessage("run setUp in picData");
54   ```
55   <!-- @[create_worker_obj](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/WorkerCallGlobalUsage.ets) -->
56
572. In Worker, use the **callGlobalCallObjectMethod** interface to call the **setUp()** method implemented in the host thread.
58
59   ```ts
60   // Worker.ets
61   import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
62   const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
63   try {
64     // Call the method without parameters.
65     let res: string = workerPort.callGlobalCallObjectMethod("picData", "setUp", 0) as string;
66     console.error("worker: ", res);
67   } catch (error) {
68     // Handle exceptions.
69     console.error("worker: error code is " + error.code + " error message is " + error.message);
70   }
71   ```
72   <!-- @[call_main_method](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/workers/Worker.ets) -->
73