• 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   <!-- @[receive_task_message](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/TaskSendDataUsage.ets) -->
16
172. Add **sendData()** to the task to enable the child thread to send messages to the host thread.
18
19   ```ts
20   // IconItemSource.ets
21   export class IconItemSource {
22     image: string | Resource = '';
23     text: string | Resource = '';
24
25     constructor(image: string | Resource = '', text: string | Resource = '') {
26       this.image = image;
27       this.text = text;
28     }
29   }
30   ```
31   <!-- @[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) -->
32
33   ```ts
34   // TaskSendDataUsage.ets
35   import { taskpool } from '@kit.ArkTS';
36   import { IconItemSource } from './IconItemSource';
37
38   // Use sendData to notify the host thread of information in real time.
39   @Concurrent
40   export function loadPictureSendData(count: number): IconItemSource[] {
41     let iconItemSourceList: IconItemSource[] = [];
42     // Add six IconItem data records.
43     for (let index = 0; index < count; index++) {
44       const numStart: number = index * 6;
45       // Use six images in the loop.
46       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
47       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
48       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
49       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
50       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
51       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
52
53       taskpool.Task.sendData(iconItemSourceList.length);
54     }
55     return iconItemSourceList;
56   }
57   ```
58   <!-- @[implement_child_thread_task](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/TaskSendDataUsage.ets) -->
59
603. In the host thread, use **onReceiveData()** to receive messages.
61   This allows the host thread to receive data sent by the task through **notice()**.
62
63   ```ts
64   // TaskSendDataUsage.ets
65   @Entry
66   @Component
67   struct Index {
68     @State message: string = 'Hello World';
69
70     build() {
71       Row() {
72         Column() {
73           Text(this.message)
74             .fontSize(50)
75             .fontWeight(FontWeight.Bold)
76             .onClick(() => {
77               let iconItemSourceList: IconItemSource[];
78               let loadPictureTask: taskpool.Task = new taskpool.Task(loadPictureSendData, 30);
79               // Use notice to receive messages from the task.
80               loadPictureTask.onReceiveData(notice);
81               taskpool.execute(loadPictureTask).then((res: object) => {
82                 iconItemSourceList = res as IconItemSource[];
83               })
84             })
85         }
86         .width('100%')
87       }
88       .height('100%')
89     }
90   }
91   ```
92   <!-- @[receive_task_data](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/TaskSendDataUsage.ets) -->
93