fixed the ensure one instanve only logic.
This commit is contained in:
@@ -2,3 +2,4 @@ keyboard==0.13.5
|
||||
pyperclip==1.9.0
|
||||
pystray==0.19.5
|
||||
Pillow==12.0.0
|
||||
psutil>=5.8.0
|
||||
|
||||
22
src/main.pyw
22
src/main.pyw
@@ -179,6 +179,11 @@ def cleanup_lock():
|
||||
|
||||
def is_process_running(pid):
|
||||
"""Check if a process with the given PID is still running."""
|
||||
try:
|
||||
import psutil
|
||||
return psutil.pid_exists(pid)
|
||||
except:
|
||||
# Fallback if psutil is not available
|
||||
try:
|
||||
os.kill(pid, 0) # Signal 0 doesn't kill, just checks if process exists
|
||||
return True
|
||||
@@ -189,6 +194,12 @@ def ensure_single_instance():
|
||||
"""Ensure only one instance of the app is running. Exit if another instance exists."""
|
||||
if os.path.exists(LOCK_FILE):
|
||||
try:
|
||||
# Read the PID from the lock file
|
||||
with open(LOCK_FILE, 'r') as f:
|
||||
old_pid = int(f.read().strip())
|
||||
|
||||
# Check if the process with that PID is still running
|
||||
if is_process_running(old_pid):
|
||||
# Another instance is running, show message and exit
|
||||
import ctypes
|
||||
ctypes.windll.user32.MessageBoxW(
|
||||
@@ -198,8 +209,17 @@ def ensure_single_instance():
|
||||
0x40 # MB_ICONINFORMATION
|
||||
)
|
||||
os._exit(1)
|
||||
else:
|
||||
# Process is not running, remove the stale lock file
|
||||
try:
|
||||
os.remove(LOCK_FILE)
|
||||
except OSError:
|
||||
pass
|
||||
except (ValueError, OSError):
|
||||
# Lock file exists but is invalid or unreadable, clean it up
|
||||
# Lock file exists but is invalid or unreadable, clean it up and continue
|
||||
try:
|
||||
os.remove(LOCK_FILE)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# Create lock file with our PID
|
||||
|
||||
Reference in New Issue
Block a user