const { app, BrowserWindow } = require('electron') const path = require('path') console.log('=== 简单单实例测试 ===') // 单实例检查 const gotTheLock = app.requestSingleInstanceLock() if (!gotTheLock) { console.log('❌ 检测到已有实例运行,退出当前实例') app.quit() } else { console.log('✅ 这是第一个实例,继续启动') let mainWindow = null // 监听第二个实例 app.on('second-instance', (event, commandLine, workingDirectory) => { console.log('检测到第二个实例启动,激活现有实例') if (mainWindow) { if (mainWindow.isMinimized()) { mainWindow.restore() } mainWindow.show() mainWindow.focus() } }) // 创建窗口 function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, contextIsolation: false } }) mainWindow.loadURL('data:text/html,

单实例测试

PID: ' + process.pid + '

时间: ' + new Date().toLocaleString() + '

') console.log('窗口已创建,PID:', process.pid) mainWindow.on('closed', () => { mainWindow = null }) } app.whenReady().then(() => { console.log('应用已准备就绪') createWindow() }) app.on('window-all-closed', () => { console.log('所有窗口已关闭') if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) }