electron应用重启,开机自启动(electron开发常用的方法、优化方案)

这篇具有很好参考价值的文章主要介绍了electron应用重启,开机自启动(electron开发常用的方法、优化方案)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

electron应用重启

不会了一定先去参考官网:electron官网
electron应用重启,开机自启动(electron开发常用的方法、优化方案),Electron,前端技巧方法,桌面开发,electron,ubuntu,javascript

主进程中监听

  ipcMain.on('window-reset', function () {
    app.relaunch()
    app.exit()
  })

页面中调用

    const restartApp = () => {
      ipcRenderer.send("window-reset");
	}

electron开机自启动

  const ex = process.execPath;
  开机是否自启动
  const isDevelopment = process.env.NODE_ENV !== "production";
  //注意:非开发环境
  if (!isDevelopment){
      if (process.platform === "darwin") {
        app.setLoginItemSettings({
          openAtLogin: true,//是否开机启动
          openAsHidden: true//是否隐藏主窗体,保留托盘位置
        });
      } else {
        app.setLoginItemSettings({
          openAtLogin: true,
          openAsHidden: true,
          path: updateExe,
          args: [
            "--processStart",
            `"${ex}"`,
            "--process-start-args",
            `"--hidden"`
          ]
        });
      }
  }

To run a local app, execute the following on the command line:

如果不启用非开发环境的话,开发者电脑开机会出现:To run a local app, execute the following on the command line: 弹框,解决方法就是开发环境不启用开机自启动,代码如上

electron应用重启 获取应用软件的安装路径

思路:用nodejs去先定时重启应用,在杀死对应软件的进程(一定要定时),不要用setTimeout 这个方法只能linux生效比较麻烦,windows中的timeout不会执行下一条命令

electron.js 主进程中写监听事件

  const ex = process.execPath;//获取应用安装路径(注意开发环境和打包环境)
  ipcMain.on('restart', (event, restart) => {
    console.log(app.getLoginItemSettings())
    if (restart) {
      //主要是这句 ex 调试时是electron路径 打包后才是运行程序路径
      app.setLoginItemSettings({ openAtLogin: true, path: ex, args: [] });
      event.sender.send('restartPath', ex);
    } else {
      app.setLoginItemSettings({ openAtLogin: false, path: ex, args: [] });
    }
  })

页面中写一个重启应用的函数
注意:这里的shelljs我用的shelljs插件,可以用node去执行cmd命令

    const restartApp = () => {
    const isWin = process.platform === 'win32';
    const isLin = process.platform === 'linux';
    const isMac = process.platform === 'darwin';

      if (isWin) {
        // ipcRenderer.send('isFullScreen', 'appQuit');
        ipcRenderer.send('restart', true);
        ipcRenderer.once('restartPath', (a: any, path: any) => {

          console.log("restartPath2:", path);
          shelljs.exec("timeout /T 1 && start " + path, (a: any, b: any, c: any) => { })
          shelljs.exec("taskkill /im BaiyiApp.exe /f", (a: any, b: any, c: any) => { })
        });
      } else if (isLin) {
        ipcRenderer.send('restart', true);
        ipcRenderer.once('restartPath', (a: any, path: any) => {
          shelljs.exec("sleep 1 && " + path, { silent: true }, (a: any, b: any, c: any) => { })
          shelljs.exec(`pkill -9 -f '${path} --enable-crashpad'`, { silent: true }, (a: any, b: any, c: any) => { })
        });

        // shelljs.exec("sleep 1 && /opt/BaiyiApp/baiyiapp", { silent: true }, (a: any, b: any, c: any) => { })
        // shelljs.exec("pkill -9 -f '/opt/BaiyiApp/baiyiapp --enable-crashpad'", { silent: true }, (a: any, b: any, c: any) => { })
      } else {
        ipcRenderer.send('isFullScreen', 'appQuit');
        ipcRenderer.send('restart', true);
        ipcRenderer.once('restartPath', (a: any, path: any) => {
          shelljs.exec("sleep 1 && " + path, { silent: true }, (a: any, b: any, c: any) => { })
          shelljs.exec(`pkill -9 -f '${path} --enable-crashpad'`, { silent: true }, (a: any, b: any, c: any) => { })
        });
      }
    }

electron中屏幕全屏,最大化,最小化,恢复,缩放

electron应用重启,开机自启动(electron开发常用的方法、优化方案),Electron,前端技巧方法,桌面开发,electron,ubuntu,javascript

electron应用重启,开机自启动(electron开发常用的方法、优化方案),Electron,前端技巧方法,桌面开发,electron,ubuntu,javascript

页面

 const originalSize = ref(1);
    const { ipcRenderer, webFrame } = require('electron');

    const scale = (type: string) => {
      isFull.value = !isFull.value;
      if (type == 'big') {
        ipcRenderer.send('isFullScreen', 'big');
      } else if (type == 'small') {
        ipcRenderer.send('isFullScreen', 'small');
      } else if (type == 'minimize') {
        ipcRenderer.send('isFullScreen', 'minimize');
      } else if (type == 'appQuit') {
        if (localStorage.getItem('TOKEN')) {
          let data = { username: store.get('username') };
          deleteToken(data).then(() => {
            ipcRenderer.send('isFullScreen', 'appQuit');
          });
        } else {
          ipcRenderer.send('isFullScreen', 'appQuit');
        }
      } else if (type == 'cutdown') {
        originalSize.value -= 0.1;
        webFrame.setZoomFactor(originalSize.value);
      } else if (type == 'addup') {
        originalSize.value += 0.1;
        webFrame.setZoomFactor(originalSize.value);
      } else if (type == 'refrsh') {
        originalSize.value = 1;
        webFrame.setZoomFactor(originalSize.value);
        location.reload();
      }
    };

主进程

  ipcMain.on('isFullScreen', function (event, type) {
    if (type == 'big') {
      mainWindow.setFullScreen(true) //全屏(包括底部的侧边栏)
    } else if (type == 'small') {
      mainWindow.setFullScreen(false) //退出全屏
    } else if (type == 'minimize') {
      mainWindow.minimize()// 最小化
    } else if (type == 'appQuit') {
      app.quit();//退出
    }else if(type == ''){
	   mainWindow.restore();// 将窗口恢复为之前的状态.
	}else if(type == 'max'){
		mainWindow.maximize()//最大化(不包含侧边栏)
	}else if(type == 'unMax'){
       mainWindow.unmaximize()//退出最大化
	}
  });

electron启动白屏时间过长优化方案

1. 按需加载模块

把不需要马上用到的模块推迟到要用时候引入,避免头部文件一次性引入过多依赖,页面加载过于臃肿(比如ui组件库,和一些js插件)

2. 窗口先创建后隐藏,初始化后再展示

有点类似于障眼法,没有从根本上去优化速度

const options = {
    width: 500,
    height: 890,
    center: true,
    title: '桌面开发App',
    icon: path.join(__dirname, 'icon.png'),

    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      // enableRemoteModule:true,
      contextIsolation: false,
      nodeIntegrationInWorker: true,
      enableRemoteModule: true,
      webSecurity: false,
      // 允许使用webview
      //   webviewTag: true
    },

	//优化速度重点在这里
    show: false, // newBrowserWindow创建后先隐藏

  }
  // Create the browser window.
  const mainWindow = new BrowserWindow(
    options
  );

  globalShortcut.register('Control+Shift+i', function () {
    mainWindow.webContents.openDevTools()
  })

  mainWindow.on('focus', () => {
    globalShortcut.register('CommandOrControl+F', function () {
      if (mainWindow && mainWindow.webContents) {
        mainWindow.webContents.send('on-find', '')
      }
    })
  })
  mainWindow.on('blur', () => {
    globalShortcut.unregister('CommandOrControl+F')
  })
	
	//优化速度重点在这里
  mainWindow.on('ready-to-show', () => {
    mainWindow.show() // 初始化后再显示
  })

3. web性能优化

electron客户端是基于h5页面开发,优雅简洁高性能代码加快启动速度

4. 优化主进程(electron.js)

使用node.js子进程,优化主进程负担,也可以new 一个Worker做线程上的处理,不过不可以和页面dom做交互,适用于用户频繁操作与缓存对比等场景文章来源地址https://www.toymoban.com/news/detail-524383.html

到了这里,关于electron应用重启,开机自启动(electron开发常用的方法、优化方案)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • windows Server【开机启动和任务计划程序】实现服务器重启后项目自启动

    有些时候我们希望计算机开机后就启动一些服务或应用程序。 使用 Win+R 调出运行,输入: 1️⃣ shell:startup 用户开机自启动(程序开机自启动只针对当前登录的用户) 打开的目录为 C:UsersAdministratorAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup 2️⃣ shell:common startup 系统开

    2024年02月08日
    浏览(43)
  • 【OpenStack】实体机断电重启后,自动启动之前是开机状态的虚拟机

    有时候服务器意外断电,恢复供电后OpenStack虚拟机都是 关闭 状态的。手动重启非常麻烦。 解决方法如下: 修改所有计算节点上的 nova.conf 配置文件 在 [DEFAULT] 中添加以下代码 重启 nova-compute 服务 至此,以后在断电,就不用手动重启之前是 运行 状态的虚拟机了。

    2024年02月11日
    浏览(36)
  • 电脑开机正常启动,运行一整子之后电脑突然关机重启报错0xc000001

    电脑自动重启或者蓝屏报错0x0000001错误导致系通无法开机或者运行不久就会重启等问题? 错误代码:0xc0000001表示引导设备可能存在某些问题,或者某些引导文件已损坏。如果计算机的引导扇区发生某些问题,也可能发生这种情况。系统文件损坏或内存损坏是比较常见的原因

    2024年02月12日
    浏览(34)
  • nginx常用命令(启动、关闭、重启)

    1、找nginx的安装位置 输入指令 whereis nginx 找到nginx安装的目录 2、进入nginx安装的目录 输入指令 cd /usr/local/nginx 进入到nginx安装的目录 3、执行需要操作的指令 关闭 启动 重启

    2024年02月02日
    浏览(28)
  • Nginx基础常用命令,版本,启动,停止,重启等

    最近部署了nginx-1.18版本,常用命令记录如下。 1.启动nginx C:javanginx-1.18.0 start nginx 或 C:javanginx-1.18.0 nginx.exe 2.停止nginx C:javanginx-1.18.0 nginx.exe -s stop 或 C:javanginx-1.18.0 nginx.exe -s quit 注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息

    2024年02月03日
    浏览(96)
  • RK3568开发板,开机后一直不断重启【Bug已解决-鸿蒙】

    在进行鸿蒙应用开发时,自己遇到过这个问题,也看到了网上大佬的解决方案,现在在这篇文章里总结一下解决方案。 问题如下: 【问题描述】 RK3568开发板,开机后一直不断重启,这是啥原因啊? 【运行环境】 硬件:RK3568 类似问题: 我用的ROCK3A和Lubancat2,都是卡DAYU,然

    2024年02月03日
    浏览(35)
  • win10,win11开机没声音重启有声音的解决方法

    win10,win11开机没声音重启有声音的解决方法 我的电脑是DELL optiplex 7060,最近安装了win11,安装DELL的官方Realtek音频驱动没任何问题,但音质太差,高清音频管理器也打不开,没法调整。安装最新的Realtek公版驱动,音质正常了,高清音频管理器也能打开了,但就是开机没声音重

    2023年04月08日
    浏览(38)
  • Electron 应用性能优化策略大全

    Electron 以其跨平台、统一开发环境的优势吸引了众多开发者投身于桌面应用的构建之中。然而,由于 Electron 应用本质上是一个结合了 Node.js 和 Chromium 浏览器的应用程序,这也意味着在享受便捷的同时,我们也必须面对潜在的性能挑战,特别是 资源消耗 和 内存管理 等问题。

    2024年04月26日
    浏览(56)
  • windows电脑关机开机后没声音,重启就有声音故障处理方法

    今天遇到一件非常奇葩的windows电脑, 从关机状态下进入系统没有声音,且从以下几点判断声卡硬件及驱动不存在故障,详见如下: 1、开机后任务栏右下角声音图标正常 2、声音设置——扬声器/麦克风(Realtek(R) Audio)正常; 3、windows+x键,选择“设备管理器”,找到“音频

    2024年02月07日
    浏览(71)
  • Windows 程序开机自启动速度优化,为什么腾讯会议自启动速度那么高?

    目录 一、问题的说明和定义 二、问题的分析 1.问题初步分析 2.详细的分析: 2.1Windows常见的自启动方式 2.2Windows常见的自启动方式的细节分析 三、问题的解决方案 1、为什么腾讯会议Rooms那么快 2.我们是否可以跟腾讯会议一样快 这两天有个优化项需要做个技术调研,就是我们

    2024年02月02日
    浏览(75)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包