等同于VB6 DoEvents的Python [英] Python Equivalent of VB6 DoEvents

查看:419
本文介绍了等同于VB6 DoEvents的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在Python等效于VB6 DoEvents语句的处理程序将临时传递到操作系统的地方?我在 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])

有更好,更Python化的方式吗?

解决方案

您的代码不执行您所说的.它为程序设置优先级.

DoEvents是VB内部的东西,它允许VB暂停其代码并在程序中运行其他代码.因此,这很危险,因为事件可能会重新进入.

有关切换到操作系统的部分在DoEvents结束时完成,它调用Windows API来调用Sleep(0),这是睡眠0秒,但将其余20 ms的时间交还给其他进程. /p>

请参阅文档: https://docs.microsoft.com/zh-cn/windows/desktop/api/synchapi/nf-synchapi-sleep

有关DoEvents的更多信息.

在VBA/VB6中,所有功能/子/属性/方法从子"到结束子"运行.当另一个子/功能正在运行时,其他子/功能将无法运行. DoEvents对此进行了更改.

在距离WordBasic等不远的地方,有人使用sendkeys键将击键发送到您正在运行的应用程序(Word for Wordbasic).但是因为您的宏正在运行Word,所以无法更新它的状态.其中一个使用DoEventsSendKeys来自动执行那些不是可编程的单词,并更新它的UI. WordBasic没有事件.

您不能更改全局变量,不能使用可能会更改的全局变量,不能更改输入参数,并且必须确保DoEvents的子/函数不能重入(或仅有限次数).否则会发生坏事,这可能是无法预料的. EG在更改选择内容的selectionchange事件中调用doevents最终将崩溃,并且堆栈空间不足.

Is there Python equivalent of VB6 DoEvents statement where the handler will temporarily pass to the OS? I found similar code in 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])

Is there better and more pythonic way?

解决方案

Your code doesn't do what you say it does. It sets priority for a program.

DoEvents is an internal VB thing that allows VB to suspend its code and run other code in your program. Thus it's dangerous because events can become reentrant.

The part about switching to the OS is done at the end of DoEvents and it calls the Windows API to call Sleep(0) which is sleep 0 seconds but surrender the rest of your 20 ms of time to other processes.

See documentation: https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-sleep

More on DoEvents.

In VBA/VB6 all functions/subs/properties/methods run from Sub to End Sub. No other sub/function can run while another is running. DoEvents changes this.

In the distance past of WordBasic etc one used sendkeys to send keystrokes to the application you were running in (Word for Wordbasic). But because your macro was running Word couldn't so didn't update it's state. One used DoEvents with SendKeys to automate things in word that weren't programmable and have it update it's UI. WordBasic didn't have events.

You can't change global variables, can't use global variables that may change, you can't change input parameters, and you have to make sure the sub/function with DoEvents isn't re-entrant (or only a limited number of times). Or bad things happen and it may not be predictable. EG calling doevents in a selectionchange event that changes the selection will eventually crash with out of stack space.

这篇关于等同于VB6 DoEvents的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆