由于 DispatcherTimer EventHandler,WPF 控件保留在内存中
Yo
我的 WPF 控件之一由于其私有成员之一而保留在内存中。罪魁祸首的成员是 DispatcherTimer,保留是因为 Tick 事件处理程序。 (此泄漏是在 ANTS Memory Profiler 工具的帮助下检测到的)
显然,我在加载/卸载时设置/删除了处理程序。并且控件已卸载......
void TransportControl_Loaded(object sender, RoutedEventArgs e)
{
if (m_playheadTimer == null)
{
m_playheadTimer = new System.Windows.Threading.DispatcherTimer();
m_playheadTimer.Tick += PlayheadTimer_Tick;
m_playheadTimer.Interval = TimeSpan.FromMilliseconds(50);
}
}
void TransportControl_Unloaded(object sender, RoutedEventArgs e)
{
if (m_playheadTimer != null)
{
if (m_playheadTimer.IsEnabled)
m_playheadTimer.Stop();
m_playheadTimer.Tick -= PlayheadTimer_Tick;
}
}
但我仍然遇到这个麻烦(就像我的控件卡在内存中一样)。 任何想法,THX
Yo
One of my WPF control is retained in memory due to one of its private member. The incriminate member is a DispatcherTimer and the retention is because of the Tick event handler. (This leak was detected with help of the tool ANTS Memory Profiler)
Obviously, I set/remove the handler on load/unload. And the control is unloaded...
void TransportControl_Loaded(object sender, RoutedEventArgs e)
{
if (m_playheadTimer == null)
{
m_playheadTimer = new System.Windows.Threading.DispatcherTimer();
m_playheadTimer.Tick += PlayheadTimer_Tick;
m_playheadTimer.Interval = TimeSpan.FromMilliseconds(50);
}
}
void TransportControl_Unloaded(object sender, RoutedEventArgs e)
{
if (m_playheadTimer != null)
{
if (m_playheadTimer.IsEnabled)
m_playheadTimer.Stop();
m_playheadTimer.Tick -= PlayheadTimer_Tick;
}
}
But still I'm stuck with that trouble (the same as my control is stuck in memory).
Any ideas, THX
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该看看弱事件模式。但这并不是一个简单的话题。
You should have a look at weak event pattern. It is not an exactly easy topic though.