correcting the one-instance only mechanism
This commit is contained in:
31
src/main.pyw
31
src/main.pyw
@@ -11,7 +11,6 @@ import re
|
|||||||
from config import load_config, save_config, get_shortcut
|
from config import load_config, save_config, get_shortcut
|
||||||
from startup import add_to_startup, remove_from_startup, is_in_startup
|
from startup import add_to_startup, remove_from_startup, is_in_startup
|
||||||
import os
|
import os
|
||||||
import signal
|
|
||||||
import atexit
|
import atexit
|
||||||
|
|
||||||
def convert_en_characters(input_str):
|
def convert_en_characters(input_str):
|
||||||
@@ -178,22 +177,32 @@ def cleanup_lock():
|
|||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def is_process_running(pid):
|
||||||
|
"""Check if a process with the given PID is still running."""
|
||||||
|
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."""
|
||||||
if os.path.exists(LOCK_FILE):
|
if os.path.exists(LOCK_FILE):
|
||||||
try:
|
try:
|
||||||
with open(LOCK_FILE, 'r') as f:
|
# Another instance is running, show message and exit
|
||||||
pid = int(f.read().strip())
|
import ctypes
|
||||||
|
ctypes.windll.user32.MessageBoxW(
|
||||||
if pid != os.getpid():
|
0,
|
||||||
try:
|
"Another instance of OopsWrongLang is already running.\n\nPlease close the existing instance first (check your system tray).",
|
||||||
os.kill(pid, signal.SIGTERM)
|
"OopsWrongLang - Already Running",
|
||||||
print(f"Killed existing instance with PID {pid}")
|
0x40 # MB_ICONINFORMATION
|
||||||
time.sleep(0.5)
|
)
|
||||||
except OSError:
|
os._exit(1)
|
||||||
pass
|
|
||||||
except (ValueError, OSError):
|
except (ValueError, OSError):
|
||||||
|
# Lock file exists but is invalid or unreadable, clean it up
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Create lock file with our PID
|
||||||
with open(LOCK_FILE, 'w') as f:
|
with open(LOCK_FILE, 'w') as f:
|
||||||
f.write(str(os.getpid()))
|
f.write(str(os.getpid()))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user