偽のthread.sleepステートメントを使用せずにWPFUIオートメーションを使用しようとしています。私がやりたいのは、コントロールが使用可能になるまで(またはタイムアウトが発生するまで)継続的にポーリングする関数GetElementByIdを用意することです。問題は、親要素の子コントロールをキャッシュしているように見えることです。子供たちにリフレッシュすることは可能ですか?または、誰かが別のアプローチを持っていますか?
public AutomationElement GetElementById(string id, int timeout)
{
if (timeout <= 1000) throw new ArgumentException("Timeout must be greater than 1000", "timeout");
AutomationElement element = null;
int timer = 0;
const int delay = 100;
do
{
element = MainWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, id));
Thread.Sleep(delay);
timer += delay;
} while (element == null && timer < timeout);
if (element == null) throw new InvalidOperationException("Unable to find element with id: " + id);
return element;
}