語言

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。

實作依賴底層的應用程式框架;因此,此方法可能會在視窗準備好供使用者輸入後一段時間內回傳。 呼叫程式碼不應依賴此方法來精確判斷視窗何時空閒。

適用於