Permalink
Browse files

add code to catch endsession message

* need to be tested on real machine with high number of torrents
  • Loading branch information...
DjLegolas committed Dec 18, 2017
1 parent a28857a commit ef6ca969413a0e7c30edc278638384be1e312429
Showing with 37 additions and 2 deletions.
  1. +37 −2 deluge/ui/gtkui/mainwindow.py
@@ -116,12 +116,47 @@ def first_show(self):
if deluge.common.windows_check():
# Handle win32 WM_QUERYENDSESSION message so GUI can be shutdown cleanly.
from win32gui import CallWindowProc, SetWindowLong
from win32con import GWL_WNDPROC, WM_QUERYENDSESSION
from win32con import GWL_WNDPROC, WM_QUERYENDSESSION, WM_ENDSESSION
from ctypes import windll, c_wchar_p
def on_wndproc(hwnd, msg, wparam, lparam):
# log.debug("%s, %s, %s", msg, wParam, lParam)
if msg == WM_QUERYENDSESSION:
reactor.stop()
log.debug("Received WM_QUERYENDSESSION, blocking shutdown")
retval = windll.user32.ShutdownBlockReasonCreate(hwnd, c_wchar_p("Shutting down Deluge"))
log.debug("Shutdown block created: %s", retval != 0)
elif msg == WM_ENDSESSION:
def quit_gtkui():
def shutdown_daemon(result):
return client.daemon.shutdown()
def disconnect_client(result):
return client.disconnect()
def stop_reactor(result):
try:
reactor.stop()
except ReactorNotRunning:
log.debug("Attempted to stop the reactor but it is not running...")
def log_failure(failure, action):
log.error("Encountered error attempting to %s: %s" % \
(action, failure.getErrorMessage()))
d = defer.succeed(None)
if client.connected():
if client.is_localhost():
d.addCallback(shutdown_daemon)
d.addErrback(log_failure, "shutdown daemon")
d.addCallback(disconnect_client)
d.addErrback(log_failure, "disconnect client")
d.addBoth(stop_reactor)
log.debug("Received WM_ENDSESSION, processing shutdown")
quit_gtkui()
retval = windll.user32.ShutdownBlockReasonDestroy(hwnd)
log.debug("Shutdown block destroyed: %s", retval != 0)
# Pass all messages on to the original WndProc.
return CallWindowProc(old_wndproc, hwnd, msg, wparam, lparam)

0 comments on commit ef6ca96

Please sign in to comment.