-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_close.py
54 lines (41 loc) · 1.41 KB
/
app_close.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import asyncio
import time
from typing import Callable
import inject
import win32api
from win32con import WM_QUIT
from main_thread_loop import execute_in_main_thread, MainExecutor
class AppCloseManager:
def __init__(self):
self._blocked_threads: list[int] = []
self._on_exit_routines: list[Callable] = []
def append_blocked_thread(self):
self._blocked_threads.append(win32api.GetCurrentThreadId())
def add_exit_handler(self, handler: Callable):
self._on_exit_routines.append(handler)
def _run_exit_handlers(self):
for handler in self._on_exit_routines:
try:
handler()
except:
pass
def setup(self):
win32api.SetConsoleCtrlHandler(self._process_exit_handler, True)
def _process_exit_handler(self, signal):
self.close()
time.sleep(1) # Give time for tray to close
return True # Prevent next handler to run
def close(self):
# Runs all exit handlers in current thread
# Then stop app from main thread
self._run_exit_handlers()
self._close()
@execute_in_main_thread(0)
def _close(self):
for thread_id in self._blocked_threads:
win32api.PostThreadMessage(
thread_id, WM_QUIT, 0, 0
)
for task in asyncio.all_tasks():
task.cancel()
inject.instance(MainExecutor).close()