【问题标题】:WinForms WebBrowser in WPF memory leakWPF 内存泄漏中的 WinForms WebBrowser
【发布时间】:2013-05-12 00:32:52
【问题描述】:

我目前正在开发一个在 WinForms 主机中使用 WinForms WebBrowser 控件的应用程序。问题是即使我关闭浏览器并处理所有内存似乎没有被释放。

我找到了这篇文章,看起来 udione 的答案可能就是我想要的。问题是我不确定如何让它通过 WPF 与 WinForms 控件一起工作。

How to get around the memory leak in the .NET Webbrowser control?

这也是我改变它的地方,但它似乎没有奏效。我不确定 this.webbrowser.webBrowser

    public void DisposeWebBrowser(System.Windows.Forms.WebBrowser browser)
    {
        //dispose to clear most of the references
        browser.Dispose();

        //REMOVED THIS LINE
        //BindingOperations.ClearAllBindings(browser.);

        //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);

        //CHANGED THIS LINE
        //var valueSwh = field.GetValue(mainwindow);   <-- OLD LINE
        var valueSwh = field.GetValue(GetWindow(this));

        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);

                //CHANGED THIS LINE
                //if (object.ReferenceEquals(sinkObject.GetValue(entry), this.webbrowser.webBrowser)) <-- OLD LINE
                if (object.ReferenceEquals(sinkObject.GetValue(entry), browser))
                {
                    ilist.Remove(entry);
                }
            }
        } 
    }

关于如何让它发挥作用或解决问题的更好方法的任何想法?

【问题讨论】:

    标签: .net wpf winforms memory-leaks browser


    【解决方案1】:

    是的。 .NET 拥有并涉及 IE 的任何 WebBrowser 都是纯粹的邪恶,不应该使用,因为它的行为因计算机而异。使用 CefSharp(首选)或 Awesomium。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      相关资源
      最近更新 更多