问题描述
很抱歉在这里提出Thunderbird,但我的C#4.0客户端框架示例应用程序存在问题,只是使用System.Windows.Automation.Automation注册了AutomationFocusChangedEventHandler.
sorry for coming up with Thunderbird here but I have a problem with my C# 4.0 client framework sample application just registering an AutomationFocusChangedEventHandler using System.Windows.Automation.Automation.
当我现在启动Mozilla Thunderbird 5.0并选择一封电子邮件时,每选择一封电子邮件,C#应用程序的非托管堆都会增长大约300kB.如果我大约每秒按下一次光标键以选择其他电子邮件,则堆会增长,并且成长和 永远不会再释放.
When I now start Mozilla Thunderbird 5.0 and select an email, the C# application's unmanaged heap grows about 300kB with each selected email. If I press the cursor key down about once a second for selecting other emails, the heap grows and grows and is never freed again.
我验证了在任务管理器的内存(私有工作集)"列中,转储了垃圾收集器的堆大小并使用SciTech .NET内存分析器对其进行了概要分析.
I verified that in task manager's column 'Memory (Private Working Set)', dumping the garbage collectors heap size and profiling it using SciTech .NET memory profiler.
这里是示例代码:
using System;
using System.Runtime.CompilerServices;
using System.Windows.Automation;
using System.Windows.Forms;
namespace MySimpleApp
{
public class UiFocusHandler : ApplicationContext
{
public UiFocusHandler()
{
AddAutomationFocusChangedEventHandler();
}
[MethodImplAttribute(MethodImplOptions.NoInlining)]
private void AddAutomationFocusChangedEventHandler()
{
Automation.AddAutomationFocusChangedEventHandler(
new AutomationFocusChangedEventHandler(OnFocusChange));
}
[MethodImplAttribute(MethodImplOptions.NoInlining)]
private void OnFocusChange(object src, AutomationFocusChangedEventArgs e)
{
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.Run(new UiFocusHandler());
}
}
}
推荐答案
IUIAutomationCacheRequest cacheRequest = _automation.CreateCacheRequest();
cacheRequest.AddProperty(_propertyIdName);
cacheRequest.AddProperty(_propertyIdBoundingRectangle);
IUIAutomationCacheRequest cacheRequest = _automation.CreateCacheRequest();
cacheRequest.AddProperty(_propertyIdName);
cacheRequest.AddProperty(_propertyIdBoundingRectangle);
//以上属性是我们所需要的,因此我们不需要引用
//接收到事件时的源元素.
cacheRequest.AutomationElementMode = AutomationElementMode.AutomationElementMode_None;
// The above properties are all we'll need, so we have have no need for a reference
// to the source element when we receive the event.
cacheRequest.AutomationElementMode = AutomationElementMode.AutomationElementMode_None;
//现在设置事件处理程序.
_automation.AddFocusChangedEventHandler(cacheRequest,this);
// Now set up the event handler.
_automation.AddFocusChangedEventHandler(cacheRequest, this);
作为测试,您可以尝试将此示例应用程序与电子邮件应用程序一起使用,以查看其是否存在相同的内存问题.
谢谢
盖伊
As a test, you might try using this sample app with the e-mail app and see if it exhibits the same memory issues.
Thanks,
Guy
这篇关于UIAutomation,AutomationFocusChangedEventHandler和Mozilla Thunderbird 5.0的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!