fix(DLPilot): 单实例锁 + 串口占用提示 —— 修复'断不开'的实际根源
This commit is contained in:
@@ -63,10 +63,30 @@ def _apply_dropdown_theme():
|
||||
ctk.ThemeManager.theme['DropdownMenu']['text_color'] = ('#1A1A1A', '#DADADA')
|
||||
|
||||
|
||||
def _ensure_single_instance():
|
||||
"""单实例锁(Windows 命名互斥体): 已有实例在运行时返回 False。
|
||||
|
||||
双击 exe 开多个实例时, 只有先启动的能连串口, 后续实例连接报"拒绝访问",
|
||||
且用户往往只看到后开的窗口 —— 表现为"连不上/断不开"。互斥体句柄随
|
||||
进程存活, 进程退出自动释放。"""
|
||||
ERROR_ALREADY_EXISTS = 183
|
||||
ctypes.windll.kernel32.CreateMutexW(None, False, 'hulk.DLPilot.single')
|
||||
return ctypes.windll.kernel32.GetLastError() != ERROR_ALREADY_EXISTS
|
||||
|
||||
|
||||
def run_gui(auto_close=0):
|
||||
"""入口: 配置外观并启动主窗口"""
|
||||
ctk.set_appearance_mode('System')
|
||||
_apply_dropdown_theme()
|
||||
if not _ensure_single_instance():
|
||||
from tkinter import messagebox
|
||||
import tkinter as tk
|
||||
r = tk.Tk()
|
||||
r.withdraw()
|
||||
messagebox.showinfo('DLPilot 已在运行',
|
||||
'检测到另一个 DLPilot 实例正在运行。\n'
|
||||
'请使用已打开的窗口(串口同时只能被一个实例占用)。')
|
||||
return
|
||||
App(auto_close).mainloop()
|
||||
|
||||
|
||||
|
||||
@@ -301,7 +301,11 @@ class DLPC485:
|
||||
self.ser = None
|
||||
with self._lock:
|
||||
self._target = None
|
||||
self.on_status(f'连接失败: {e}')
|
||||
et = str(e)
|
||||
if 'PermissionError' in repr(e) or '拒绝访问' in et \
|
||||
or 'Access is denied' in et:
|
||||
et += '(串口被占用: 检查串口助手/另一个本工具实例)'
|
||||
self.on_status(f'连接失败: {et}')
|
||||
if self.ser is not None:
|
||||
try:
|
||||
item = None
|
||||
|
||||
Reference in New Issue
Block a user