1# Worker Thread Synchronously Calling Methods of the Host Thread 2 3If the Worker thread needs to call the method that has been implemented in the main thread, you can perform the following operations: 4 5The following uses an example in which the worker synchronously calls the host thread interface for description. 6 71. First, implement the method in the host thread, create a **Worker** object, and register the method 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 // Six images are used cyclically. 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 method on the Worker object. 51 workerInstance.registerGlobalCallObject("picData", picData); 52 workerInstance.postMessage("run setUp in picData"); 53 ``` 54 552. Then, the setUp () method in the host thread can be called through the callGlobalCallObjectMethod interface in the worker. 56 57 ```ts 58 // Worker.ets 59 import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 60 const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 61 try { 62 // The method to call does not carry an input parameter. 63 let res: string = workerPort.callGlobalCallObjectMethod("picData", "setUp", 0) as string; 64 console.error("worker: ", res); 65 } catch (error) { 66 // Exception handling. 67 console.error("worker: error code is " + error.code + " error message is " + error.message); 68 } 69 ``` 70