fixed the ensure one instanve only logic.
This commit is contained in:
@@ -2,3 +2,4 @@ keyboard==0.13.5
|
|||||||
pyperclip==1.9.0
|
pyperclip==1.9.0
|
||||||
pystray==0.19.5
|
pystray==0.19.5
|
||||||
Pillow==12.0.0
|
Pillow==12.0.0
|
||||||
|
psutil>=5.8.0
|
||||||
|
|||||||
32
src/main.pyw
32
src/main.pyw
@@ -180,15 +180,26 @@ def cleanup_lock():
|
|||||||
def is_process_running(pid):
|
def is_process_running(pid):
|
||||||
"""Check if a process with the given PID is still running."""
|
"""Check if a process with the given PID is still running."""
|
||||||
try:
|
try:
|
||||||
os.kill(pid, 0) # Signal 0 doesn't kill, just checks if process exists
|
import psutil
|
||||||
return True
|
return psutil.pid_exists(pid)
|
||||||
except OSError:
|
except:
|
||||||
return False
|
# Fallback if psutil is not available
|
||||||
|
try:
|
||||||
|
os.kill(pid, 0) # Signal 0 doesn't kill, just checks if process exists
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
|
||||||
def ensure_single_instance():
|
def ensure_single_instance():
|
||||||
"""Ensure only one instance of the app is running. Exit if another instance exists."""
|
"""Ensure only one instance of the app is running. Exit if another instance exists."""
|
||||||
if os.path.exists(LOCK_FILE):
|
if os.path.exists(LOCK_FILE):
|
||||||
try:
|
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
|
# Another instance is running, show message and exit
|
||||||
import ctypes
|
import ctypes
|
||||||
ctypes.windll.user32.MessageBoxW(
|
ctypes.windll.user32.MessageBoxW(
|
||||||
@@ -198,9 +209,18 @@ def ensure_single_instance():
|
|||||||
0x40 # MB_ICONINFORMATION
|
0x40 # MB_ICONINFORMATION
|
||||||
)
|
)
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
else:
|
||||||
|
# Process is not running, remove the stale lock file
|
||||||
|
try:
|
||||||
|
os.remove(LOCK_FILE)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
except (ValueError, OSError):
|
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
|
||||||
pass
|
try:
|
||||||
|
os.remove(LOCK_FILE)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
# Create lock file with our PID
|
# Create lock file with our PID
|
||||||
with open(LOCK_FILE, 'w') as f:
|
with open(LOCK_FILE, 'w') as f:
|
||||||
|
|||||||
Reference in New Issue
Block a user