public static WindowActivityMonitorInfo GetForegroundWindowInfo()
{
const int maxTries = 3;
var monitorInfo = new WindowActivityMonitorInfo();
// the reason we try a few times is because UI
// automation sometimes throws an exception depending
// the state of the focused window.
for (int ii = 0; ii < maxTries; ii++)
{
try
{
monitorInfo.FgHwnd = Windows.GetForegroundWindow();
monitorInfo.FocusedElement = AutomationElement.FocusedElement;
monitorInfo.Title = Windows.GetWindowTitle(monitorInfo.FgHwnd);
monitorInfo.FgProcess = GetProcessForWindow(monitorInfo.FgHwnd);
break;
}
catch
{
System.Threading.Thread.Sleep(100);
}
}
return monitorInfo;
}