• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Communication Between the TaskPool Task and Host Thread
2
3When a task needs to do more than just return a final result—such as periodically updating the host thread on its status, reporting data changes, or returning large volumes of data in segments (for example, fetching large datasets from a database)—you can use the approach described in this topic.
4
5The following example uses multiple image loading tasks that provide real-time updates to illustrate the process.
6
71. 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("Child thread task completed. Total images loaded:", data)
13   }
14   ```
15
162. In the task, use **sendData()** to send messages to the host 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 host thread of information in real time.
37   @Concurrent
38   export function loadPictureSendData(count: number): IconItemSource[] {
39     let iconItemSourceList: IconItemSource[] = [];
40     // Add six IconItem data records.
41     for (let index = 0; index < count; index++) {
42       const numStart: number = index * 6;
43       // Use six images in the loop.
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. In the host thread, use **onReceiveData()** to receive messages.
58   This allows the host thread to receive 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 from 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