【发布时间】:2014-09-02 15:20:49
【问题描述】:
我正在编写一个程序,它将我的浏览历史记录在一个文本文件中。它从 chrome 窗口中获取 url 并将其写入文本文件。它获取 chrome 窗口句柄作为参数并将 url 写入 out 参数。代码如下所示:
static AutomationElement elm;// = AutomationElement.FromHandle(handle);
static AutomationElement elmUrlBar;
public static void GetChromeUrl(IntPtr handle, out string url)
{
string namedProperty = "Address and search bar" ;
url = null;
elm = AutomationElement.FromHandle(handle);
elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, namedProperty));
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
url = val.Current.Value;
}
}
}
我每隔 5 秒使用计时器回调调用此方法,它会从 chrome 浏览器窗口返回正确的 url。所以它工作得很好,尽管它似乎没有释放由 AutomationElement 对象占用的内存,并且此应用程序使用的 RAM 不断增长。我使用 dotMemory 4.0 和 ant memory profiler 8 对其进行了分析,它表明automationElement 对象已创建但从未被垃圾收集器删除。有人知道如何解决这个问题吗?
【问题讨论】:
-
如果您要重构代码并将 AutomationElement 对象包装在
using(){}语句周围会发生什么?你也可以实现IDisposible吗?你也可以打电话给GC.Collect()
标签: c# .net browser memory-leaks ui-automation