Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angular应用程序中从主线程终止一组web Worker?_Angular_Typescript_Web Worker - Fatal编程技术网

如何在angular应用程序中从主线程终止一组web Worker?

如何在angular应用程序中从主线程终止一组web Worker?,angular,typescript,web-worker,Angular,Typescript,Web Worker,我正在开发一个angular应用程序,它可以创建多个web worker,并在worker线程中执行单独的工作。我的应用程序通过单击按钮读取输入的文件,然后根据文件中给定的[array.length]创建新的工作进程。它工作得很好。现在,我需要做的是通过单击停止按钮来终止所有web Worker。下面是我在两次单击按钮时使用的两种方法 public i = 0; public file: any; public noOfOrders: number; private worker: Worker

我正在开发一个angular应用程序,它可以创建多个web worker,并在worker线程中执行单独的工作。我的应用程序通过单击按钮读取输入的文件,然后根据文件中给定的[array.length]创建新的工作进程。它工作得很好。现在,我需要做的是通过单击停止按钮来终止所有web Worker。下面是我在两次单击按钮时使用的两种方法

public i = 0;
public file: any;
public noOfOrders: number;
private worker: Worker;

public uploadDocument(): void {
    const fileReader = new FileReader();
    fileReader.onload = (e) => {
        const k = JSON.parse(fileReader.result.toString());
        this.noOfOrders = Math.round(k.orders_per_second / k.credentials.length);

        if (typeof Worker !== 'undefined') {
            while (this.i < k.credentials.length) {
                this.worker = new Worker('./app.worker', { type: 'module' });
                      this.worker.onmessage = ({ data }) => {
                        console.log(data + k.credentials.length);
                      };
                      this.worker.postMessage({ data: k.credentials[this.i], orders: this.noOfOrders });
                      this.i++;
            }
        }
    };
    fileReader.readAsText(this.file);
}

public stop(): void {                    // this method only terminates the final worker thread
    this.worker.terminate();
}
public i=0;
公共档案:任何;
公共无福特车:数量;
私人工人:工人;
公共上载文档():void{
const fileReader=new fileReader();
fileReader.onload=(e)=>{
const k=JSON.parse(fileReader.result.toString());
this.noOfOrders=Math.round(k.orders\u per\u second/k.credentials.length);
如果(工作人员类型!==“未定义”){
while(此.i{
console.log(数据+k.credentials.length);
};
this.worker.postMessage({data:k.credentials[this.i],orders:this.noOfOrders});
这个.i++;
}
}
};
fileReader.readAsText(this.file);
}
public stop():void{//此方法仅终止最后一个工作线程
this.worker.terminate();
}

每次都覆盖同一个辅助进程,因此只能终止最后一个辅助进程

相反,将它们存储在工作线程数组中,并对所有工作线程调用
terminate()
。大概是这样的:

public i=0;
公共档案:任何;
公共无福特车:数量;
私人工人:工人[];
公共上载文档():void{
const fileReader=new fileReader();
fileReader.onload=(e)=>{
const k=JSON.parse(fileReader.result.toString());
this.noOfOrders=Math.round(k.orders\u per\u second/k.credentials.length);
如果(工作人员类型!==“未定义”){
while(此.i{
console.log(数据+k.credentials.length);
};
postMessage({data:k.credentials[this.i],orders:this.noOfOrders});
这个。工人。推(工人);
这个.i++;
}
}
};
fileReader.readAsText(this.file);
}
公共停止():无效{
this.workers.forEach(w=>w.terminate());
}

将它们存储在数组中,然后您就可以终止所有它们