PostQuitMessage之后如何重新启动pythoncom.PumpMessages?

问题描述

我使用钩子检查鼠标事件和键盘事件,并为该钩子创建新线程,因为我还有其他工作要做

钩子线程一直消耗cpu,因此我想偶尔暂停它(以提高效率)。我尝试使用PostQuitMessage()将其停止,然后等待在正确的时间重新启动线程和PumpMessages()。但这不起作用。

我期待收到您的答复!

下面是代码的主要部分,您可以运行它,然后会出现问题。 ps。打扰一下英语〜_〜

# -*- coding: utf-8 -*- #
#Python version:3.5.4
from win32 import win32process
import time
import threading
#import win32
import psutil
import PyHook3 as pyHook
import pythoncom
import win32gui,win32ui,win32con,win32api

def OnMouseEvent(event):
    print('-----Mouse-----',event)
    return True
def OnKeyboardEvent(event):
    print('-----key-----',event)
    if str(event.Key)=='F12':   
        win32api.PostQuitMessage()
    return True

def InputScan():
    print('-----InputScan-----')
    hm.MouseLeftDown = OnMouseEvent
    hm.HookMouse()
    hm.KeyDown = OnKeyboardEvent
    hm.HookKeyboard()
    #first time is worked normally,but re-execution not working
    pythoncom.PumpMessages() 
    return True


print('start program...')
threads = []
NowThreadsName=[]
hm = pyHook.HookManager()
try:
    Threadis = threading.Thread(target=InputScan,name='Threadis',args=())
    Threadis.setDaemon(True)
    threads.append(Threadis)
    Threadis.start()
except:
    print("Error:--")
while 1:
    #some other works
    time.sleep(5)
    open_str = input("whether restart(anykey): \n")
    Nowthread = threading.enumerate()
    NowThreadsName.clear()
    print("NowThreadsNameclear=",NowThreadsName)
    for i in Nowthread:
        NowThreadsName.append(i.getName())
    print("NowThreadsName=",NowThreadsName)
    if 'Threadis' in NowThreadsName:
        pass
    else:
        t = threading.Thread(target=InputScan,args=())
        t.setDaemon(True)
        t.start() #why it dosen't work??
        pass
    pass

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)