From 5291bbd5961b0639770e5ec8d3415872831fa0f8 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sun, 6 Oct 2024 14:31:51 +0800 Subject: [PATCH] fix trafficMonitor not stop --- changelog.md | 8 ++------ src/main/resolve/trafficMonitor.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index b2a902c..edf244b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,8 +1,4 @@ -### New Features - -- 允许切换订阅卡片显示过期时间还是更新时间 -- 添加悬浮窗功能,可以在设置中开启 - ### Bug Fixes -- 修复某些 Windows 管理员权限无法正常启动的问题 +- 修复开启轻量模式后 TrafficMonitor 重复启动的问题 +- 修复 TrafficMonitor 重复开关后位置偏移的问题 diff --git a/src/main/resolve/trafficMonitor.ts b/src/main/resolve/trafficMonitor.ts index 77c121c..21fee11 100644 --- a/src/main/resolve/trafficMonitor.ts +++ b/src/main/resolve/trafficMonitor.ts @@ -1,12 +1,24 @@ import { ChildProcess, spawn } from 'child_process' import { getAppConfig } from '../config' -import { resourcesFilesDir } from '../utils/dirs' +import { dataDir, resourcesFilesDir } from '../utils/dirs' import path from 'path' +import { existsSync } from 'fs' +import { readFile, rm, writeFile } from 'fs/promises' let child: ChildProcess export async function startMonitor(detached = false): Promise { if (process.platform !== 'win32') return + if (existsSync(path.join(dataDir(), 'monitor.pid'))) { + const pid = parseInt(await readFile(path.join(dataDir(), 'monitor.pid'), 'utf-8')) + try { + process.kill(pid, 'SIGINT') + } catch { + // ignore + } finally { + await rm(path.join(dataDir(), 'monitor.pid')) + } + } await stopMonitor() const { showTraffic = true } = await getAppConfig() if (!showTraffic) return @@ -16,6 +28,9 @@ export async function startMonitor(detached = false): Promise { stdio: detached ? 'ignore' : undefined }) if (detached) { + if (child && child.pid) { + await writeFile(path.join(dataDir(), 'monitor.pid'), child.pid.toString()) + } child.unref() } }