• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7*     http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import taskpool from '@ohos.taskpool'
17import router from '@ohos.router'
18
19function functionForTasks(numberOfExecutions: number) {
20  "use concurrent"
21  return numberOfExecutions + 1
22}
23
24@Component
25export struct TaskPool {
26  @State numberOfExecutions: number = 0;
27  private isExecute = true;
28  private clickAble = true;
29  @State task: taskpool.Task | null = null;
30  @State msg: string = 'task ready';
31
32  build() {
33    Column() {
34      Row() {
35        Text($r('app.string.click_exec_add_one'))
36          .margin({ top: 22 })
37      }
38      .width('90%')
39
40      Column() {
41        Row() {
42          Text('numberOfExecutions:')
43            .fontColor($r('sys.color.ohos_id_color_text_primary'))
44          Text(this.numberOfExecutions.toString())
45            .fontColor($r('sys.color.ohos_id_color_text_primary'))
46        }
47        .width('100%')
48
49        Row() {
50          Text('task status:')
51            .fontColor($r('sys.color.ohos_id_color_text_primary'))
52          Text(this.msg)
53            .fontColor($r('sys.color.ohos_id_color_text_primary'))
54        }
55        .width('100%')
56      }
57      .height(160)
58      .margin({ top: 12 })
59      .padding(16)
60      .borderRadius(16)
61      .backgroundColor($r('app.color.bg_white'))
62
63      Blank()
64        .layoutWeight(1)
65
66      Row({ space: 20 }) {
67        Button('Execute task')
68          .id('execute_task')
69          .type(ButtonType.Capsule)
70          .width('45%')
71          .fontColor($r('app.color.text_color_accent'))
72          .backgroundColor($r('app.color.bg_btn_grey'))
73          .onClick(async () => {
74            if (!this.clickAble) {
75              return;
76            }
77            for (let i = 0; i > -1; i++) {
78              this.task = new taskpool.Task(functionForTasks, i);
79              if (!this.isExecute) {
80                this.isExecute = true;
81                taskpool.cancel(this.task);
82              } else {
83                this.clickAble = false;
84                this.msg = 'task is running';
85                await taskpool.execute(this.task)
86                  .then((res: Object) => {
87                    this.numberOfExecutions = Number(JSON.parse(JSON.stringify(res)));
88                  });
89              }
90            }
91          })
92
93        Button('Cancel task')
94          .id('cancel_task')
95          .type(ButtonType.Capsule)
96          .width('45%')
97          .fontColor($r('app.color.text_color_red'))
98          .backgroundColor($r('app.color.bg_btn_grey'))
99          .onClick(() => {
100            if (!this.clickAble) {
101              this.msg = 'task cancel';
102              this.clickAble = true;
103              this.isExecute = !this.isExecute;
104            }
105          })
106      }
107      .width('100%')
108      .margin({ top: 12, bottom: 16 })
109      .justifyContent(FlexAlign.Center)
110    }
111    .width('100%')
112    .height('100%')
113    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
114  }
115}