You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
3 years ago
|
"""
|
||
5 years ago
|
# How to use
|
||
3 years ago
|
# from pyOpenRPA.Tools import StopSafe
|
||
|
# StopSafe.Init(inLogger=None)
|
||
|
# StopSafe.IsSafeStop() # True - WM_CLOSE SIGNAL has come. taskkill /im someprocess.exe
|
||
|
"""
|
||
|
|
||
5 years ago
|
|
||
|
import win32con
|
||
|
import win32gui
|
||
|
import os
|
||
|
gIsSignalCloseBool = False
|
||
|
gLogger = None
|
||
|
gWindowTitleStr = "PythonTerminator" # Title of the phantom window
|
||
|
gWindowDescriptionStr = "pyOpenRPA library for safe turn off the program (by send the WM_CLOSE signal from task kill)" # Description of the phantom window
|
||
|
|
||
|
def Init(inLogger=None):
|
||
3 years ago
|
"""
|
||
|
Init the StopSafe module. After that you can use def IsStopSafe() to check if close signal has come.
|
||
|
|
||
|
:param inLogger: Logger to log messages about StopSafe
|
||
|
:return:
|
||
|
"""
|
||
5 years ago
|
global gLogger
|
||
|
global gIsSignalCloseBool
|
||
|
gIsSignalCloseBool = False # Init default
|
||
|
gLogger = inLogger
|
||
|
import threading
|
||
3 years ago
|
if gLogger: gLogger.info(f"StopSafe: Init termination catch thread")
|
||
|
shutdown_thread = threading.Thread(target=_shutdown_monitor)
|
||
5 years ago
|
shutdown_thread.start()
|
||
|
#shutdown_thread.join()
|
||
|
#shutdown_monitor()
|
||
|
|
||
3 years ago
|
|
||
|
def IsStopSafe():
|
||
|
"""
|
||
|
Check if stop signal has come.
|
||
|
|
||
|
:return:
|
||
|
"""
|
||
5 years ago
|
global gIsSignalCloseBool # Init the global variable
|
||
|
return gIsSignalCloseBool # Return the result
|
||
|
|
||
3 years ago
|
def _shutdown_monitor():
|
||
5 years ago
|
global gIsSignalCloseBool # Init the global variable
|
||
|
global gLogger
|
||
|
def wndproc(hwnd, msg, wparam, lparam):
|
||
|
if msg == win32con.WM_CLOSE:
|
||
|
win32gui.DestroyWindow(hwnd)
|
||
|
return 0
|
||
|
elif msg == win32con.WM_DESTROY:
|
||
|
win32gui.PostQuitMessage(0)
|
||
|
return 0
|
||
|
return win32gui.DefWindowProc(hwnd, msg, wparam, lparam)
|
||
|
wc = win32gui.WNDCLASS()
|
||
|
wc.lpszClassName = gWindowTitleStr
|
||
|
wc.lpfnWndProc = wndproc
|
||
|
win32gui.RegisterClass(wc)
|
||
|
hwnd = win32gui.CreateWindow(gWindowTitleStr, gWindowDescriptionStr,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0, None)
|
||
|
win32gui.PumpMessages()
|
||
|
gIsSignalCloseBool = True # WM_CLOSE message has come
|
||
|
if gLogger:
|
||
3 years ago
|
gLogger.info(f"StopSafe: Program has catch VM_CLOSE signal - do safe exit")
|
||
5 years ago
|
|