宿主线程如何向worker子线程发送消息?

宿主线程如何向worker子线程发送消息?

HarmonyOS
2024-07-23 10:16:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
社恐的小美
//宿主线程代码:
import worker from '@ohos.worker';
workerInstance: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Myworker.ts');
Button("发消息给子线程").onClick(() => {
  this.workerInstance.postMessage({ name: "Tom" })
})
//Myworker子线程中接收消息
workerPort.onmessage = (e: MessageEvents) => {
  console.log("========================接收到消息"+JSON.stringify(e))
}
分享
微博
QQ
微信
回复
2024-07-23 18:51:44