我们正在使用UIAutomation来监视浏览器的URL,但是我们观察到的是,每当我们开始捕获URL时,浏览器就会使用高CPU,并且它没有响应。我的应用程序如何影响浏览器以使用高cpu。在所有最新的浏览器中,只有我们之前从未遇到过这种问题。
这是我用来获取网址的代码
///--------------------------------------------------------------------------------
/// <summary>
/// Get URL by using AutomatioElement.
/// </summary>
/// <param name="process"></param>
/// <param name="logPrefix"></param>
/// <returns>Url</returns>
private string GetProcessURL(Process process, string logPrefix)
{
AutomationElement UrlBarElement = null;
AutomationElement mainWindowElement = null;
AutomationElement rootElement = null;
try
{
mainWindowElement = AutomationElement.FromHandle(process.MainWindowHandle);
if (mainWindowElement == null)
{
log.Warn(logPrefix + " - Unable to capture the URL as MainWindowElement is Null.");
return null;
}
switch (process.ProcessName)
{
case ApplicationConstants.MicrosoftEdge:
rootElement = AutomationElement.FromHandle(GetDesktopWindow());
foreach (AutomationElement child in rootElement.FindAll(TreeScope.Children, PropertyCondition.TrueCondition))
{
AutomationElement window = GetEdgeCommandsWindow(child);
if (window == null) // not edge
continue;
return GetEdgeUrl(window);
}
break;
case ApplicationConstants.Opera:
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.OperaNameProperty));
break;
case ApplicationConstants.Chrome:
Condition chromeConditions = new OrCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.ChromeNameProperty));
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.ChromeNameProperty));
break;
case ApplicationConstants.FireFox:
Condition fireFoxConditions = new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.FireFoxNameProperty),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, fireFoxConditions);
if (UrlBarElement == null)
{
fireFoxConditions = new OrCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, fireFoxConditions);
}
break;
default:
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
}
//Comment Before Deployment.
//Thread.Sleep(5000);
if (UrlBarElement != null)
{
return ((ValuePattern)UrlBarElement.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
else
{
log.Info(logPrefix + " - Unable to capture the URL because unable to detect the URLBarelement");
return null;
}
}
catch (Exception exx)
{
}
finally
{
mainWindowElement = null;
process = null;
UrlBarElement = null;
rootElement = null;
//GC.Collect();
}
return null;
}