HarmonyOS Sendable装饰的对象怎么获取

" @Sendable
export class Test {}

export async function startThread(): Promise<toolTask.Task> {
  let currTask = new toolTask.Task(test);
  await toolTask.execute(currTask, toolTask.Priority.MEDIUM).then(result => {
    let test = result as Test;//
  }) as boolean;
  return currTask;
}

@Concurrent
async function test(): Promise<Test> {
  return new Test();
}"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
HarmonyOS
2024-12-10 14:22:53
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考如下demo,通过参数传递

// SendableTest.ets

@Sendable
export class Test {
  num: number = 10;
}


// Index.ets

import taskpool from '@ohos.taskpool';
import { Test } from './SendableTest';


@Concurrent
async function testFunc(test: Test): Promise<void> {
  let tmp = new Test();
  tmp.num = 1;
  test.num = tmp.num;
}

async function start(): Promise<taskpool.Task> {
  let test = new Test();
  test.num = 101;
  console.info("taskpoolTest:res1 is:" + test.num);
  let currTask = new taskpool.Task(testFunc, test);
  await taskpool.execute(currTask, taskpool.Priority.MEDIUM).then(() => {
    console.info("taskpoolTest:res2 is:" + test.num);
  }) as boolean
  return currTask;
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            start();
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
分享
微博
QQ
微信
回复
2024-12-10 15:20:43
相关问题
HarmonyOS taskpool Sendable对象
669浏览 • 1回复 待解决
HarmonyOS 怎么获取VpnExtensionContext对象
434浏览 • 1回复 待解决
HarmonyOS sendable崩溃问题
572浏览 • 1回复 待解决
HarmonyOS 怎么自定义装饰
759浏览 • 1回复 待解决
HarmonyOS 如何获取对象方法列表
1143浏览 • 1回复 待解决
HarmonyOS 如何获取Resource对象
771浏览 • 1回复 待解决
如何获取对象真实类型
1205浏览 • 1回复 待解决
HarmonyOS 对象怎么判空?
1848浏览 • 1回复 待解决
HarmonyOS sendable支持范围问题
630浏览 • 1回复 待解决
HarmonyOS 使用了@Sendable注解类报错
1125浏览 • 1回复 待解决