# 等待長時間操作
GUI應用程序行為通常不穩定,您的腳本需要等待,直到出現新窗口或關閉/隱藏現有窗口。 pywinauto可以隱式地(默認超時)靈活地等待對話框初始化,或者明確地使用專用方法/函數來幫助您使代碼更容易和更可靠。
## Application 方法
> * [wait_cpu_usage_lower](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html?highlight=wait_cpu_usage_lower#pywinauto.application.Application.wait_cpu_usage_lower) (new in pywinauto 0.5.2, renamed in 0.6.0)
此方法對于多線程接口很有用,當GUI響應且所有控件都已存在且似乎已準備好使用時,允許在另一個線程中進行延遲初始化。因此,等待特定窗口的存在/狀態是無用的。在這種情況下,整個進程的CPU使用率表示任務計算尚未完成。
范例:
~~~
app.wait_cpu_usage_lower(threshold=5) # 等到CPU使用率低于5%
~~~
>[info]注意:此方法僅適用于整個應用程序進程,不適用于窗口/元素。
## WindowSpecification 方法
These methods are available to all controls.
> * [wait](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html?highlight=wait#pywinauto.application.WindowSpecification.wait)
> * [wait\_not](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html?highlight=wait_not#pywinauto.application.WindowSpecification.wait_not)
有一個包含長等待的例子: [install script for 7zip 9.20 x64](https://gist.github.com/vasily-v-ryabov/7a04717af4584cbb840f).
`WindowSpecification`對象不一定與現有窗口/控件相關。 這只是一個描述,即搜索窗口的幾個標準。 `wait`方法(如果沒有引發任何異常)可以保證目標控件存在,甚至可見,啟用和/或激活。
## `timings`模塊中的函數
還有一些對任何Python代碼都有用的低級方法。
> * [wait\_until](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.timings.html?highlight=wait_until#pywinauto.timings.wait_until)
> * [wait\_until\_passes](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.timings.html?highlight=wait_until_passes#pywinauto.timings.wait_until_passes)
如果每個函數調用都應該有時序控制,也可以使用裝飾器[`pywinauto.timings.always_wait_until()`](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.timings.html#pywinauto.timings.always_wait_until "pywinauto.timings.always_wait_until")和[`pywinauto.timings.always_wait_until_passes()`](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.timings.html#pywinauto.timings.always_wait_until_passes "pywinauto.timings.always_wait_until_passes")
~~~
# call ensure_text_changed(ctrl) every 2 sec until it's passed or timeout (4 sec) is expired
@always_wait_until_passes(4, 2)
def ensure_text_changed(ctrl):
if previous_text == ctrl.window_text():
raise ValueError('The ctrl text remains the same while change is expected')
~~~
## Global timings for all actions
許多動作需要在之前,之后和之間暫停。 模塊`timings`中有幾個全局常量定義了這種暫停。 通過在對象`timings.Timings`中設置全局靜態變量,它可以單獨根據您的需要進行調整。
所有全局計時可以一次設置為默認值,或者加倍,或者除以2:
~~~
from timings import Timings
Timings.defaults()
Timings.slow() # double all timings (~2x slower script execution)
Timings.fast() # divide all timings by two (~2x faster)
~~~
## 識別控件
幫助您找到所需控件的方法。
> * [print_control_identifiers](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html?highlight=print_control_identifiers#pywinauto.application.WindowSpecification.print_control_identifiers)
> * [draw_outline](https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.hwndwrapper.html?highlight=draw_outline#pywinauto.controls.hwndwrapper.HwndWrapper.draw_outline)
### 如何
* [如何](HowTo.md)
- 什么是Pywinauto
- 入門指南
- 如何
- 等待長時間操作
- 遠程執行指南
- 每種不同控制類型可用的方法
- 貢獻者
- 開發筆記
- 待辦項目
- 更新日志
- 基本用戶輸入模塊
- pywinauto.mouse
- pywinauto.keyboard
- 主要用戶模塊
- pywinauto.application
- pywinauto.findbestmatch
- pywinauto.findwindows
- pywinauto.timings
- 特定功能
- pywinauto.clipboard
- pywinauto.win32_hooks
- 控件參考
- pywinauto.base_wrapper
- pywinauto.controls.hwndwrapper
- pywinauto.controls.menuwrapper
- pywinauto.controls.common_controls
- pywinauto.controls.win32_controls
- pywinauto.controls.uiawrapper
- pywinauto.controls.uia_controls
- Pre-supplied Tests
- pywinauto.tests.allcontrols
- pywinauto.tests.asianhotkey
- pywinauto.tests.comboboxdroppedheight
- pywinauto.tests.comparetoreffont
- pywinauto.tests.leadtrailspaces
- pywinauto.tests.miscvalues
- pywinauto.tests.missalignment
- pywinauto.tests.missingextrastring
- pywinauto.tests.overlapping
- pywinauto.tests.repeatedhotkey
- pywinauto.tests.translation
- pywinauto.tests.truncation
- 后端內部實施模塊
- pywinauto.backend
- pywinauto.element_info
- pywinauto.win32_element_info
- pywinauto.uia_element_info
- pywinauto.uia_defines
- 內部模塊
- pywinauto.controlproperties
- pywinauto.handleprops
- pywinauto.xml_helpers
- pywinauto.fuzzydict
- pywinauto.actionlogger
- pywinauto.sysinfo
- pywinauto.remote_memory_block