ImageKnife/entry/src/main/ets/pages/testMultiThreadWorkerPage2.ets

78 lines
2.8 KiB
Plaintext

/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import ArkWorker from '@ohos.worker'
@Entry
@Component
struct TestMultiThreadWorkerPage2 {
build() {
Scroll() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button("创建Worker向子线程发送携带Arraybuffer数据")
.margin({ top: 20 })
.onClick(() => {
let worker = new ArkWorker.Worker("entry/ets/pages/workers/worker1.js", {
type: "classic",
name: "zhangsan"
})
worker.onerror = function (data) {
console.info("worker:: receive onerror " + data.lineno + ", msg = " + data.message + ", filename = " + data.filename + ", colno = " + data.colno);
}
worker.onmessageerror = function (e) {
console.log("worker:: receive onmessageerror ");
}
worker.onexit = function () {
console.log("worker:: receive onexit");
}
worker.onmessage = function (e) {
var data = e.data;
switch (data.type) {
case "normal":
console.log("worker:: onmessage " + data.data);
break;
case "buffer":
console.log("worker:: receive buffer length is " + data.data.byteLength);
break;
default:
console.log("worker:: worker.js receive unknow type");
break
}
worker.terminate();
}
console.log("worker:: start post buffer");
let uint8array = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7])
let buffer = uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteLength + uint8array.byteOffset)
var obj = { type: "buffer", data: buffer };
console.log("worker:: before post, buffer length is " + obj.data.byteLength);
worker.postMessage(obj, [buffer]);
console.log("worker:: after post, buffer length is " + obj.data.byteLength);
console.info("end post buffer");
});
}
}
.width('100%')
.height('100%')
}
}