zoukankan      html  css  js  c++  java
  • electron使用new Worker写入文件导致浏览器崩溃

    main.js

    let data1 = [];
    let data2 = [];
    for (let i = 0;i < 500000;i++) {
        let j = i * 500.0 / 60000.0;
        data1.push([j, 200 * Math.random() + 100]);
        data2.push([j, 400 * Math.random() + 100]);
    }
    let params = {
        type: 'xls',
        data: { data1, data2 },
        filename: 'text.xls',
    };
    let worker = new Worker('worker.js');
    worker.onmessage = (ev) => {
    console.log('写入完成'); worker.terminate(); }; worker.postMessage(
    params);

    worker.js

    const fs = require( "fs" );
    
    self.onmessage = function (e) {
        let data = e.data;
        console.log(data);
        if (data.type === "xls") {
            //todo 写入文件流
            let writeStream = fs.createWriteStream(data.filename, { encoding: 'utf8' });
            //读取文件发生错误事件
            writeStream.on('error', (err) => {
                console.error('发生异常:', err);
           self.postMessage({ success: false, message: err }); });
    //已打开要写入的文件事件 writeStream.on('open', (fd) => { console.log('文件已打开:', fd); let i = 0; let value = "x y y1 "; function write() { if (i >= data.data.data1.length) { writeStream.end(); setTimeout(() => {//todo 不知道为什么,立即发送报错,延迟发送没问题,这样electron窗口就不会崩溃 self.postMessage({ success: true, message: 'complete' }); }, 500); return; } value = data.data.data1[i][0] + " " + data.data.data1[i][1] + " " + data.data.data2[i][1] + " ";
              i++; writeStream.write(value, ()
    => { write(); }); } writeStream.write(value, () => { write(); }); }); //文件已经就写入完成事件 writeStream.on('finish', () => { console.log('写入已完成..'); }); //文件关闭事件 writeStream.on('close', () => { console.log('文件已关闭!'); }); } };
  • 相关阅读:
    android xml属性大全
    Activity 中传递数据
    android 界面布局 很好的一篇总结
    Android基础类之BaseAdapter
    自定义android的tab样式
    android之Uri的常用几个例子
    我的第一篇博客
    [解题报告]272 TEX Quotes
    [解题报告]10071 Back to High School Physics
    [解题报告]113 Power of Cryptography
  • 原文地址:https://www.cnblogs.com/caiyingyong/p/11597964.html
Copyright © 2011-2022 走看看