jrv*_*ant 5 .net browser memory wpf memory-leaks
这段代码是由 udione 给出的,用于回应有关 .Net 中 WebBrowser 控件内存泄漏的长期问题。
//dispose to clear most of the references
this.webbrowser.Dispose();
BindingOperations.ClearAllBindings(this.webbrowser);
//using reflection to remove one reference that was not removed with the dispose
var field = typeof(System.Windows.Window).GetField("_swh", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var valueSwh = field.GetValue(mainwindow);
var valueSourceWindow = valueSwh.GetType().GetField("_sourceWindow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSwh);
var valuekeyboardInput = valueSourceWindow.GetType().GetField("_keyboardInputSinkChildren", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSourceWindow);
System.Collections.IList ilist = valuekeyboardInput as System.Collections.IList;
lock(ilist)
{
for (int i = ilist.Count-1; i >= 0; i--)
{
var entry = ilist[i];
var sinkObject = entry.GetType().GetField("_sink", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (object.ReferenceEquals(sinkObject.GetValue(entry), this.webbrowser.webBrowser))
{
ilist.Remove(entry);
}
}
}
Run Code Online (Sandbox Code Playgroud)
1)第三行,
BindingOperations.ClearAllBindings(this.webbrowser);
Run Code Online (Sandbox Code Playgroud)
不会为我编译。的类型是什么this.webbrowser?我原以为是这样WebBrowser,但方法需要System.Windows.DependencyObject。
2)在行中
var valueSwh = field.GetValue(mainwindow);
Run Code Online (Sandbox Code Playgroud)
什么是mainwindow?持有浏览器控制权的表单?
3)在倒数第六行中,
if (object.ReferenceEquals(sinkObject.GetValue(entry), this.webbrowser.webBrowser))
Run Code Online (Sandbox Code Playgroud)
的类型是什么this.webbrowser.webBrowser?webBrowser我看到类型中没有调用任何字段WebBrowser。这只是一个错字吗?
谢谢你的帮助。
BindingOperations适用于 WPF - 如果您使用 WinForms,则不需要此行。mainwindow,您只需调用 WPF 方法即可GetWindow。 var mainwindow = GetWindow(this);
Run Code Online (Sandbox Code Playgroud)
3.this.webbrowser是 WPF 控件的控件 ID ( FrameworkElement.Name)。默认情况下,这通常是webbrowser1.
| 归档时间: |
|
| 查看次数: |
357 次 |
| 最近记录: |