Electron: 渲染进程开启子线程 Worker

zz/2023/9/24 23:06:21

在Vue中需要使用例外的线程去执行轮询的任务,这里以获取网卡信息为例。

注:这里是在Electron中使用的,可以使用Node.js实例 “child_process”,如果是纯Vue工程无法使用Node实例。 

1.创建一个renderer.js文件,放在public静态文件夹下。

这里以获取网卡信息为例:

// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.var cluster = require('child_process')setInterval(() => {cluster.exec("cmd /c chcp 65001>nul && netsh wlan show interface",  (err, res) => {var node_nics = require("os").networkInterfaces();var lines = res.split('\r\n');var fields = ['name','model','vendor','mac_address','status','ssid','bssid','mode','radio','authentication','encryption','connection','channel','reception','transmission','signal','profil'];var connections = [];var tmp = {}var len = 0;for (var i = 3; i < lines.length; i++) {if (lines[i] != "" && lines[i] != null && lines[i].indexOf(":") != -1) {tmp[fields[len]] = lines[i].split(':')[1].trim()len += 1;} else {if (tmp['name'] != null && tmp['model'] != null && tmp['vendor'] != null && tmp['mac_address'] != null && tmp['status'] != null) {var node_nic = node_nics[tmp.name] || [];node_nic.forEach(function (type) {if (type.family == 'IPv4') {tmp.ip_address = type.address;tmp.ip_gateway = "http://"+type.address.split('.')[0] + "." + type.address.split('.')[1] + "." + type.address.split('.')[2] + ".1"}});connections.push(tmp);tmp = {}len = 0;}}}console.log(connections)self.postMessage(connections);})
}, 2000)

2.home.vue

var worker = new Worker("/renderer.js");
export default { name: "home",data() {return {lists: ""}},created(){var that = this;worker.onmessage = function(event) {this.lists = event.data;}},
}


http://www.ngui.cc/zz/2543037.html

相关文章

vue前端遮罩层问题

在Vue的开发中&#xff0c;一旦我们用到对话框&#xff0c;经常出现的问题是对话框被遮罩层挡住&#xff0c;无论是Element-UI dialog还是bootstrap的Modal&#xff0c;如下图所示&#xff1a;造成这个问题的原因是对话框组件的父元素的position有fixed或者relative值&#xff…

字符串常用的14种方法,记得关注收藏

字符串常用的14种方法&#xff0c;记得收藏 1、将字符串转换成char数组 ToCharArray() 2、将char数组转换成字符串 new string(char[]); 3、将字符串转换成小写 ToLower() 4、将字符串转换成大写 ToUpper() 5、比较两个字符串的时候&#xff0c;忽略大小写 S1.Equals(s2,…); 6…

关于数据库的查询(交叉连接,内连接,外连接-左外连接,外连接-右外连接,全连接)

1.交叉连接 select * from 表1 as 别名1 cross join 表2 as 别名2 说明1&#xff1a; 不带WHERE条件子句&#xff0c;它将会返回被连接的两个表的笛卡尔积&#xff0c;返回结果的 行数等于两个表行数的乘积 2.内连接 select * from 表1 as 别名1 inner join 表2 as 别名2 …

打开照相机和相册

step1:修改xml文件. <LinearLayoutandroid:orientation"vertical"android:layout_width"match_parent"android:layout_height"match_parent"><Buttonandroid:id"id/take_photo"android:layout_width"match_parent"…

String字符编码格式

String.getBytes();//得到一个操作系统默认的编码格式的字节数组. String.getBytes(“UTF-8”);//根据指定的decode编码返回某字符串在该编码下的 byte数组表示. new String(btye[] b, “UTF-8”);//按照指定的方法编码 2.编码解码 String info1"中";byte[] info2 …

Large Dual Encoders Are Generalizable Retrievers

Large Dual Encoders Are Generalizable Retrievers(arXiv) 原文地址&#xff1a;https://arxiv.org/pdf/2112.07899.pdf Motivation 之前的研究发现&#xff0c;在一个领域上训练的双塔模型通常不能泛化到其他领域的检索任务。一种普遍的看法是双塔模型的bottleneck layer …

EasyExcel保姆级教程(1)

由于博猪所在行业使用到关于Excel导入、导出的功能比较多&#xff0c;本文主要详细介绍一下博猪使用的阿里巴巴的easyExcel&#xff0c;让Excel相关操作没那么多烦恼。 前言 Excel的缺点 Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是…

华为云文字识别接口

在华文云提供的文档基础上进行了简单的整理&#xff0c;需要引入华为相关的jar包 import com.oss.oss_demo.util.OcrClientToken; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import …

交换排序之冒泡排序

冒泡排序的思路是通过比较邻近两个元素的值的大小&#xff0c;然后判断是否进行交换&#xff0c;在冒泡排序时每一次都会有一个元素到达应该到的位置。 #include <iostream>using namespace std;void swap(int &a, int &b);void BSort(int arr[], int num);int …

springboot整合mybatis-plus逆向工程,一键生成controller-service-entity-mapper

1.创建一个springboot工程&#xff0c;加入一下依赖&#xff0c;在application.propertites里配置数据库连接等信息 <!--支持全栈式Web开发&#xff0c;包括Tomcat和spring-webmvc-> <dependency><groupId>org.springframework.boot</groupId><art…