• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Synchronous Calls to Host Thread Interfaces from Worker
2
3If 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.
4
5The following example demonstrates how Worker can synchronously call an interface implemented in 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
22   ```ts
23   // WorkerCallGlobalUsage.ets
24   import worker from '@ohos.worker';
25   import { IconItemSource } from './IconItemSource';
26
27   // Create a Worker object.
28   const workerInstance: worker.ThreadWorker = new worker.ThreadWorker("entry/ets/pages/workers/Worker.ts");
29
30   class PicData {
31     public iconItemSourceList: IconItemSource[] = [];
32
33     public setUp(): string {
34       for (let index = 0; index < 20; index++) {
35         const numStart: number = index * 6;
36         // Use six images in the loop.
37         this.iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
38         this.iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
39         this.iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
40         this.iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
41         this.iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
42         this.iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
43
44       }
45       return "setUpIconItemSourceList success!";
46     }
47   }
48
49   let picData = new PicData();
50   // Register the object to be called on the Worker object.
51   workerInstance.registerGlobalCallObject("picData", picData);
52   workerInstance.postMessage("run setUp in picData");
53   ```
54
552. In Worker, use the **callGlobalCallObjectMethod** interface to call the **setUp()** method implemented in the host thread.
56
57   ```ts
58   // Worker.ets
59   import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
60   const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
61   try {
62     // Call the method without parameters.
63     let res: string = workerPort.callGlobalCallObjectMethod("picData", "setUp", 0) as string;
64     console.error("worker: ", res);
65   } catch (error) {
66     // Handle exceptions.
67     console.error("worker: error code is " + error.code + " error message is " + error.message);
68   }
69   ```
70