WindowPattern.WaitForInputIdle(Int32) 方法

定义

在指定时间阻止或在关联进程进入空闲状态之前持续阻止调用代码。

public:
 bool WaitForInputIdle(int milliseconds);
public bool WaitForInputIdle (int milliseconds);
member this.WaitForInputIdle : int -> bool
Public Function WaitForInputIdle (milliseconds As Integer) As Boolean

参数

milliseconds
Int32

等待关联进程变为空闲状态的时间(以毫秒为单位)。 最大值为 Int32.MaxValue

返回

如果窗口已进入空闲状态,则为 true;如果发生超时,则为 false

例外

传入的参数不是有效数字。

示例

在以下示例中 WindowPattern ,从 AutomationElement 获取控件模式,并使用 WaitForInputIdle 确认 元素是否已准备好在合理的时间内进行用户交互。

///--------------------------------------------------------------------
/// <summary>
/// Obtains a WindowPattern control pattern from an automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A WindowPattern object.
/// </returns>
///--------------------------------------------------------------------
private WindowPattern GetWindowPattern(AutomationElement targetControl)
{
    WindowPattern windowPattern = null;

    try
    {
        windowPattern =
            targetControl.GetCurrentPattern(WindowPattern.Pattern)
            as WindowPattern;
    }
    catch (InvalidOperationException)
    {
        // object doesn't support the WindowPattern control pattern
        return null;
    }
    // Make sure the element is usable.
    if (false == windowPattern.WaitForInputIdle(10000))
    {
        // Object not responding in a timely manner
        return null;
    }
    return windowPattern;
}
'''------------------------------------------------------------------------
''' <summary>
''' Obtains a WindowPattern control pattern from an automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A WindowPattern object.
''' </returns>
'''------------------------------------------------------------------------
Private Function GetWindowPattern(ByVal targetControl As AutomationElement) As WindowPattern
    Dim windowPattern As WindowPattern = Nothing

    Try
        windowPattern = DirectCast( _
        targetControl.GetCurrentPattern(windowPattern.Pattern), _
        WindowPattern)
    Catch
        ' object doesn't support the WindowPattern control pattern
        Return Nothing
    End Try
    ' Make sure the element is usable.
    If False = windowPattern.WaitForInputIdle(10000) Then
        ' Object not responding in a timely manner
        Return Nothing
    End If
    Return windowPattern
End Function 'GetWindowPattern

注解

此方法通常与 的处理 WindowOpenedEvent结合使用。

实现依赖于基础应用程序框架;因此,此方法可能会在窗口准备好供用户输入后返回一段时间。 调用代码不应依赖此方法来确定窗口何时变为空闲状态。

适用于