Python 等效于 VB6 DoEvents
Python Equivalent of VB6 DoEvents
是否有 Python 等效于 VB6 DoEvents 语句的处理程序将临时传递给 OS?我在 http://code.activestate.com/recipes/496767-set-process-priority-in-windows/
中找到了类似的代码
def setpriority(pid=None,priority=1):
""" Set The Priority of a Windows Process. Priority is a value between 0-5 where
2 is normal priority. Default sets the priority of the current
python process but can take any valid process ID. """
import win32api,win32process,win32con
priorityclasses = [win32process.IDLE_PRIORITY_CLASS,
win32process.BELOW_NORMAL_PRIORITY_CLASS,
win32process.NORMAL_PRIORITY_CLASS,
win32process.ABOVE_NORMAL_PRIORITY_CLASS,
win32process.HIGH_PRIORITY_CLASS,
win32process.REALTIME_PRIORITY_CLASS]
if pid == None:
pid = win32api.GetCurrentProcessId()
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
win32process.SetPriorityClass(handle, priorityclasses[priority])
有没有更好更pythonic的方式?
你的代码没有按照你说的做。它设置程序的优先级。
DoEvents
是一个内部 VB 东西,它允许 VB 暂停它的代码和 运行 程序中的其他代码。因此这很危险,因为事件可以重入。
关于切换到 OS 的部分在 DoEvents
结束时完成,它调用 Windows API 调用 Sleep(0)
这是休眠 0 秒,但将剩余的 20 毫秒时间交给其他进程。
查看文档:
https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-sleep
更多关于 DoEvents
。
In VBA/VB6 所有 functions/subs/properties/methods 运行 从 Sub 到 End Sub。没有其他 sub/function 可以 运行 而另一个是 运行ning。 DoEvents
改变了这一点。
在 WordBasic 等很久以前,人们使用 sendkeys
将击键发送到您 运行 正在使用的应用程序(Word for Wordbasic)。但是因为您的宏是 运行ning Word 无法更新它的状态。一个使用 DoEvents
和 SendKeys
来自动化 word 中不可编程的东西,并让它更新它 UI。 WordBasic 没有事件。
你不能改变全局变量,不能使用可能改变的全局变量,你不能改变输入参数,你必须确保sub/function和DoEvents
是不可重入(或只能重入有限次数)。或者坏事发生并且可能无法预测。在更改选择的 selectionchange
事件中调用 doevents
的 EG 最终会因堆栈不足而崩溃 space。
是否有 Python 等效于 VB6 DoEvents 语句的处理程序将临时传递给 OS?我在 http://code.activestate.com/recipes/496767-set-process-priority-in-windows/
中找到了类似的代码def setpriority(pid=None,priority=1):
""" Set The Priority of a Windows Process. Priority is a value between 0-5 where
2 is normal priority. Default sets the priority of the current
python process but can take any valid process ID. """
import win32api,win32process,win32con
priorityclasses = [win32process.IDLE_PRIORITY_CLASS,
win32process.BELOW_NORMAL_PRIORITY_CLASS,
win32process.NORMAL_PRIORITY_CLASS,
win32process.ABOVE_NORMAL_PRIORITY_CLASS,
win32process.HIGH_PRIORITY_CLASS,
win32process.REALTIME_PRIORITY_CLASS]
if pid == None:
pid = win32api.GetCurrentProcessId()
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
win32process.SetPriorityClass(handle, priorityclasses[priority])
有没有更好更pythonic的方式?
你的代码没有按照你说的做。它设置程序的优先级。
DoEvents
是一个内部 VB 东西,它允许 VB 暂停它的代码和 运行 程序中的其他代码。因此这很危险,因为事件可以重入。
关于切换到 OS 的部分在 DoEvents
结束时完成,它调用 Windows API 调用 Sleep(0)
这是休眠 0 秒,但将剩余的 20 毫秒时间交给其他进程。
查看文档: https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-sleep
更多关于 DoEvents
。
In VBA/VB6 所有 functions/subs/properties/methods 运行 从 Sub 到 End Sub。没有其他 sub/function 可以 运行 而另一个是 运行ning。 DoEvents
改变了这一点。
在 WordBasic 等很久以前,人们使用 sendkeys
将击键发送到您 运行 正在使用的应用程序(Word for Wordbasic)。但是因为您的宏是 运行ning Word 无法更新它的状态。一个使用 DoEvents
和 SendKeys
来自动化 word 中不可编程的东西,并让它更新它 UI。 WordBasic 没有事件。
你不能改变全局变量,不能使用可能改变的全局变量,你不能改变输入参数,你必须确保sub/function和DoEvents
是不可重入(或只能重入有限次数)。或者坏事发生并且可能无法预测。在更改选择的 selectionchange
事件中调用 doevents
的 EG 最终会因堆栈不足而崩溃 space。