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();
}"
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%')
  }
}
分享
微博
QQ
微信
回复
2024-12-10 15:20:43
相关问题
HarmonyOS taskpool Sendable对象
114浏览 • 1回复 待解决
HarmonyOS 怎么获取VpnExtensionContext对象
113浏览 • 1回复 待解决
HarmonyOS 怎么自定义装饰
194浏览 • 1回复 待解决
HarmonyOS sendable崩溃问题
52浏览 • 1回复 待解决
HarmonyOS 如何获取对象方法列表
587浏览 • 1回复 待解决
如何获取对象真实类型
645浏览 • 1回复 待解决
HarmonyOS 如何获取Resource对象
311浏览 • 1回复 待解决
HarmonyOS 对象怎么判空?
955浏览 • 1回复 待解决
HarmonyOS sendable支持范围问题
150浏览 • 1回复 待解决