重启之后清除登录信息
This commit is contained in:
87
test-single-instance.js
Normal file
87
test-single-instance.js
Normal file
@@ -0,0 +1,87 @@
|
||||
const { app } = require('electron');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
console.log('开始测试单实例锁功能...');
|
||||
|
||||
// 模拟单实例锁检查
|
||||
function testSingleInstanceLock() {
|
||||
const userDataPath = app.getPath('userData');
|
||||
const lockFile = path.join(userDataPath, 'single-instance-lock');
|
||||
|
||||
console.log('用户数据路径:', userDataPath);
|
||||
console.log('锁文件路径:', lockFile);
|
||||
|
||||
// 检查是否存在锁文件
|
||||
if (fs.existsSync(lockFile)) {
|
||||
console.log('发现锁文件,尝试读取内容...');
|
||||
try {
|
||||
const content = fs.readFileSync(lockFile, 'utf8');
|
||||
console.log('锁文件内容:', content);
|
||||
|
||||
// 检查进程是否还在运行
|
||||
const pid = parseInt(content.trim());
|
||||
console.log('锁文件中的 PID:', pid);
|
||||
|
||||
// 在 Windows 上检查进程
|
||||
if (process.platform === 'win32') {
|
||||
const { execSync } = require('child_process');
|
||||
try {
|
||||
const result = execSync(`tasklist /FI "PID eq ${pid}" /FO CSV /NH`, { encoding: 'utf8' });
|
||||
if (result.includes(pid.toString())) {
|
||||
console.log(`进程 ${pid} 仍在运行`);
|
||||
} else {
|
||||
console.log(`进程 ${pid} 不存在,可以清理锁文件`);
|
||||
fs.unlinkSync(lockFile);
|
||||
console.log('锁文件已清理');
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`进程 ${pid} 不存在,清理锁文件`);
|
||||
fs.unlinkSync(lockFile);
|
||||
console.log('锁文件已清理');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('读取锁文件失败:', error);
|
||||
}
|
||||
} else {
|
||||
console.log('没有找到锁文件');
|
||||
}
|
||||
}
|
||||
|
||||
// 测试创建锁文件
|
||||
function testCreateLock() {
|
||||
const userDataPath = app.getPath('userData');
|
||||
const lockFile = path.join(userDataPath, 'single-instance-lock');
|
||||
|
||||
try {
|
||||
fs.writeFileSync(lockFile, process.pid.toString());
|
||||
console.log(`创建锁文件: ${lockFile}, PID: ${process.pid}`);
|
||||
} catch (error) {
|
||||
console.error('创建锁文件失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
console.log('当前进程 PID:', process.pid);
|
||||
testSingleInstanceLock();
|
||||
|
||||
// 如果直接运行此脚本
|
||||
if (require.main === module) {
|
||||
// 创建测试锁文件
|
||||
testCreateLock();
|
||||
|
||||
// 等待一段时间后清理
|
||||
setTimeout(() => {
|
||||
console.log('清理测试锁文件...');
|
||||
const userDataPath = app.getPath('userData');
|
||||
const lockFile = path.join(userDataPath, 'single-instance-lock');
|
||||
if (fs.existsSync(lockFile)) {
|
||||
fs.unlinkSync(lockFile);
|
||||
console.log('测试锁文件已清理');
|
||||
}
|
||||
process.exit(0);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
module.exports = { testSingleInstanceLock, testCreateLock };
|
||||
Reference in New Issue
Block a user