1# Communication Between the TaskPool Task and Host Thread 2 3If a subthread needs to periodically notify the main thread of the task status and data changes, or needs to return a large amount of data by segment (for example, a large amount of data read from the database), you can perform the following operations: 4 5The following uses an example in which results of a plurality of image loading tasks are returned in real time. 6 71. First, implement a method to receive messages sent by the task. 8 9 ```ts 10 // TaskSendDataUsage.ets 11 function notice(data: number): void { 12 console.info("The subthread task has been executed. Total images loaded:", data) 13 } 14 ``` 15 162. Then, add **sendData()** to the task to enable the subthread to send messages to the main thread. 17 18 ```ts 19 // IconItemSource.ets 20 export class IconItemSource { 21 image: string | Resource = ''; 22 text: string | Resource = ''; 23 24 constructor(image: string | Resource = '', text: string | Resource = '') { 25 this.image = image; 26 this.text = text; 27 } 28 } 29 ``` 30 31 ```ts 32 // TaskSendDataUsage.ets 33 import { taskpool } from '@kit.ArkTS'; 34 import { IconItemSource } from './IconItemSource'; 35 36 // Use sendData to notify the main thread of information in real time. 37 @Concurrent 38 export function loadPictureSendData(count: number): IconItemSource[] { 39 let iconItemSourceList: IconItemSource[] = []; 40 // Traverse and add six IconItem data records. 41 for (let index = 0; index < count; index++) { 42 const numStart: number = index * 6; 43 // Six images are used cyclically. 44 iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`)); 45 iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`)); 46 iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`)); 47 iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`)); 48 iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`)); 49 iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`)); 50 51 taskpool.Task.sendData(iconItemSourceList.length); 52 } 53 return iconItemSourceList; 54 } 55 ``` 56 573. Finally, use **onReceiveData()** to enable the main thread to receive messages. 58 In this way, the main thread can receive the data sent by the task through **notice()**. 59 60 ```ts 61 // TaskSendDataUsage.ets 62 @Entry 63 @Component 64 struct Index { 65 @State message: string = 'Hello World'; 66 67 build() { 68 Row() { 69 Column() { 70 Text(this.message) 71 .fontSize(50) 72 .fontWeight(FontWeight.Bold) 73 .onClick(() => { 74 let iconItemSourceList: IconItemSource[]; 75 let lodePictureTask: taskpool.Task = new taskpool.Task(loadPictureSendData, 30); 76 // Use notice to receive messages sent by the task. 77 lodePictureTask.onReceiveData(notice); 78 taskpool.execute(lodePictureTask).then((res: object) => { 79 iconItemSourceList = res as IconItemSource[]; 80 }) 81 }) 82 } 83 .width('100%') 84 } 85 .height('100%') 86 } 87 } 88 ``` 89