python vb6_相当于VB6 DoEvents的Python

1586010002-jmsa.png

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值