🐛 保证应用单例运行

This commit is contained in:
MoeSnowyFox
2025-08-27 20:29:39 +08:00
parent 163cb78fc9
commit b5df09cfd2
3 changed files with 32 additions and 4 deletions

View File

@@ -70,10 +70,10 @@ function restartAsAdmin() {
// 使用PowerShell以管理员权限启动 // 使用PowerShell以管理员权限启动
(0, child_process_1.spawn)('powershell', [ (0, child_process_1.spawn)('powershell', [
'-Command', '-Command',
`Start-Process -FilePath "${exePath}" -ArgumentList "${args.join(' ')}" -Verb RunAs` `Start-Process -FilePath "${exePath}" -ArgumentList "${args.join(' ')}" -Verb RunAs`,
], { ], {
detached: true, detached: true,
stdio: 'ignore' stdio: 'ignore',
}); });
electron_1.app.quit(); electron_1.app.quit();
} }
@@ -262,6 +262,19 @@ electron_1.ipcMain.handle('restart-as-admin', () => {
restartAsAdmin(); restartAsAdmin();
}); });
// 应用生命周期 // 应用生命周期
// 保证应用单例运行
const gotTheLock = electron_1.app.requestSingleInstanceLock();
if (!gotTheLock) {
electron_1.app.quit();
process.exit(0);
}
electron_1.app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized())
mainWindow.restore();
mainWindow.focus();
}
});
electron_1.app.whenReady().then(() => { electron_1.app.whenReady().then(() => {
// 检查管理员权限 // 检查管理员权限
if (!isRunningAsAdmin()) { if (!isRunningAsAdmin()) {

View File

@@ -34,5 +34,5 @@ electron_1.contextBridge.exposeInMainWorld('electronAPI', {
}, },
removeDownloadProgressListener: () => { removeDownloadProgressListener: () => {
electron_1.ipcRenderer.removeAllListeners('download-progress'); electron_1.ipcRenderer.removeAllListeners('download-progress');
} },
}); });

View File

@@ -271,6 +271,21 @@ ipcMain.handle('restart-as-admin', () => {
}) })
// 应用生命周期 // 应用生命周期
// 保证应用单例运行
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
process.exit(0);
}
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
app.whenReady().then(() => { app.whenReady().then(() => {
// 检查管理员权限 // 检查管理员权限
if (!isRunningAsAdmin()) { if (!isRunningAsAdmin()) {
@@ -279,7 +294,7 @@ app.whenReady().then(() => {
// 这里先创建窗口,让用户选择是否重新启动 // 这里先创建窗口,让用户选择是否重新启动
} }
createWindow() createWindow()
}) });
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit() if (process.platform !== 'darwin') app.quit()